Jquery 为什么我的字符串不能用作URL?
我使用时间戳生成URL,如下所示:Jquery 为什么我的字符串不能用作URL?,jquery,Jquery,我使用时间戳生成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
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);
}
然后,我使用返回的url调用我的json函数,但它正在检索实时数据。有什么问题吗?尝试在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中使用
希望能有所帮助我做了一个小测试来查看功能,对我来说似乎很正常
<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>
$().ready(函数()){
$('#getLatest')。单击(函数(){
console.log(“来到这里”);
$.ajax({
cache:false,
键入:“获取”,
async:false,
url:writeDate(),//正在获取url,但未检索实时数据
数据类型:“jsonp”,
成功:功能(msg){
$(msg).每个(函数(i,值){
seArray[i]=value.CurrentPrice;
})
showResult();
},
错误:函数(xhr){
console.log('update:'+msg);
}
});
});
var writeDate=函数(){
var x=新日期();
year=x.getFullYear();
月份=(x.getMonth()+1)<10?'0'+(x.getMonth()+1):(x.getMonth()+1);
date=x.getDate()<10?'0'+x.getDate():x.getDate();
时间=x.getHours()<10?'0'+x.getHours():x.getHours();
分钟=x.getMinutes()<10?'0'+x.getMinutes():x.getMinutes();
秒=x.getSeconds()<10?'0'+x.getSeconds():x.getSeconds();
//这只是为了调试
警报(“这是调用时间戳“+年+”-“+月+”-“+日期+”+时间+”:“+分钟+”:“+秒+”.000”)之前的调试警报);
var timeStamp=String(年+'-'+月+'-'+日期+'+时间+':'+分钟+':'+秒+'.000');
警报(“这是调用时间戳后的调试警报”+时间戳);
返回字符串(“http://107.20.173.235/BlufinAPI/Service/WhackAScrip.svc/GetWASOneMinuteSensexDatawithPosition?TimeStamp=“+时间戳);
}
});
获取最新日期
我刚刚添加了秒数,以确保日期正在更改,但请注意,使用Javascript时,此日期是客户端日期,如果您想要服务器端日期,最好根据服务器时间而不是客户端时间给出日期
问候,,
Gabriel您是否尝试查看writeDate()返回的内容?console.info是您的朋友。
<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>