为什么我的字符串不能用作URL?

时间:2012-03-13 06:23:21

标签: jquery

我正在使用Timestamp生成一个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函数,但它检索实时数据是不是错了?

2 个答案:

答案 0 :(得分:1)

尝试在writeDate函数中执行 encodeURI 以获取时间戳,例如:


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中使用。

希望有所帮助

答案 1 :(得分: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,如果您想要服务器端日期,您最好根据服务器时间而不是客户端给出日期时间。

此致 加布里埃尔