2012-03-13 36 views
0

我生成使用時間戳這樣的URL:爲什麼我的字符串不能用作URL?

var writeDate = function(){ 
    var x = new Date(); 
    year = x.getFullYear(); 
    month = (x.getMonth()+1) < 10 ? '0'+(x.getMonth()+1) : (x.getMonth()+1); 
    date = x.getDate() < 10 ? '0'+x.getDate() : x.getDate(); 
    time = x.getHours() < 10 ? '0'+x.getHours() : x.getHours(); 
    minute = x.getMinutes() < 10 ? '0'+x.getMinutes() : x.getMinutes(); 

    var timeStamp = String(year+'-'+month+'-'+date+' '+time+':'+minute+':00.000'); 
    return String("http://107.20.173.235/BlufinAPI/Service/WhackAScrip.svc/GetWASOneMinuteSensexDatawithPosition?TimeStamp="+timeStamp); 

} 

//使用函數

$.ajax({ 
      cache: false, 
      type: "GET", 
      async: false, 
      url: writeDate(),//getting URL but not retrieving the live data 
      dataType: "jsonp", 
      success: function (msg) { 
       $(msg).each(function(i,value){ 
        seArray[i]=value.CurrentPrice; 
       }) 
       showResult(); 
      }, 
      error: function (xhr) { 
        console.log('update:'+msg); 
      } 

然後調用,我使用返回的URL調用我的JSON功能,但它的檢索實時數據這是錯的嗎?

+1

你試過看什麼writeDate()返回? console.info是你的朋友。 – Thilo 2012-03-13 06:25:27

回答

1

嘗試做是encodeURI在writeDate功能時間戳,如:

 

var writeDate = function(){ 
    .... 
    ..... 
    var timeStamp = String(year+'-'+month+'-'+date+' '+time+':'+minute+':00.000'); 
    timeStamp = encodeURI(timeStamp); 
    return String("http://107.20.173.235/BlufinAPI/Service/WhackAScrip.svc /GetWASOneMinuteSensexDatawithPosition?TimeStamp="+timeStamp); 
} 
 

然後在AJAX URL中使用。

希望它可以幫助

+0

是的,它的工作原理。謝謝。 – 3gwebtrain 2012-03-13 06:47:31

0

我做了一個小測試,看看功能,它似乎正常的,我

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"  type="text/javascript"></script> 
<script type="text/javascript"> 
$().ready(function() { 
    $('#getLatest').click(function() { 
     console.log("came here"); 
     $.ajax({ 
      cache: false, 
      type: "GET", 
      async: false, 
      url: writeDate(),//getting URL but not retrieving the live data 
      dataType: "jsonp", 
      success: function (msg) { 
       $(msg).each(function(i,value){ 
        seArray[i]=value.CurrentPrice; 
       }) 
       showResult(); 
      }, 
      error: function (xhr) { 
        console.log('update:'+msg); 
      } 
     }); 
    }); 
    var writeDate = function(){ 
     var x = new Date(); 
     year = x.getFullYear(); 
     month = (x.getMonth()+1) < 10 ? '0'+(x.getMonth()+1) : (x.getMonth()+1); 
     date = x.getDate() < 10 ? '0'+x.getDate() : x.getDate(); 
     time = x.getHours() < 10 ? '0'+x.getHours() : x.getHours(); 
     minute = x.getMinutes() < 10 ? '0'+x.getMinutes() : x.getMinutes(); 
     seconds = x.getSeconds() < 10 ? '0'+x.getSeconds() : x.getSeconds(); 

      //This is just for debug 
     alert("This is debug alert before calling timestamp " + year+'-'+month+'-'+date+' '+time+':'+minute+':'+seconds+'.000'); 
     var timeStamp = String(year+'-'+month+'-'+date+' '+time+':'+minute+':'+seconds+'.000'); 
     alert("This is debug alert after calling timestamp " + timeStamp); 
     return String("http://107.20.173.235/BlufinAPI/Service/WhackAScrip.svc/GetWASOneMinuteSensexDatawithPosition?TimeStamp="+timeStamp); 
    } 
}); 
</script> 
</head> 
<body> 
<button id="getLatest">Get Latest Date</button> 
</body> 
</html> 

我剛添加的秒,以確保日期發生了變化,但被勸這個日期是客戶端日期作爲您使用的Javascript,如果您想要服務器端日期,您最好根據服務器時間而不是客戶端時間給出日期。

問候, 加布裏埃爾

相關問題