Javascript 在jQuery中创建动态日期列表
在javascript中,我需要一个简单的日期列表,如(对于jquery日期选择器): 因为这个列表是动态的,所以我想使用这个ajax调用:Javascript 在jQuery中创建动态日期列表,javascript,ajax,django,Javascript,Ajax,Django,在javascript中,我需要一个简单的日期列表,如(对于jquery日期选择器): 因为这个列表是动态的,所以我想使用这个ajax调用: var disabledDays = $.ajax({ type: 'GET', url: "/disabled-days", }); 在Django中,我编写了这个测试视图,它以json格式返回数据: def disabled_days(request, soort): from django.http i
var disabledDays = $.ajax({
type: 'GET',
url: "/disabled-days",
});
在Django中,我编写了这个测试视图,它以json格式返回数据:
def disabled_days(request, soort):
from django.http import HttpResponse
import json
soort = ["16-5-2013", "24-5-2013", "25-5-2013", "26-5-2013", "27-5-2013"]
return HttpResponse(json.dumps(soort), mimetype="application/json")
这里的json是传递数据的方式吗?我在ResponseText中找到了日期列表,但无法访问此数据
例如,我试图对以下项目进行迭代:
for (i = 0; i < disabledDays1.length; i++) {
item = results[i];
disabledDays.push(item);
}
执行console.log()时,我会看到以下信息:
GET http://localhost:8080/disabled-days
200 OK
39ms
jquery-1.9.1.js (regel 8526)
Object { readyState=1, getResponseHeader=function(), getAllResponseHeaders=function(), meer...}
以下是Anh Tú给出的解决方案(尚未生效):
var disabledDays1=$.ajax({
键入:“GET”,
url:“/disabled days”,
});
var disabledDays=[]
对于(i=0;i
我认为您处理ajax请求时出错了。通常,当请求成功时,您会声明一个回调函数,并按如下方式处理接收到的数据:
var disabledDays = []
$.ajax({
type: 'GET',
url: "/disabled-days",
success: function(data){
// data is the response json
disabledDays = data
// do whatever it needs to be done with disabledDays,
// it should be the array of dates.
// the process should be inside this method because of asynchronous
// javascript method execution
}
});
希望有帮助 给我看看你的responseText@AnhTú我已经编辑了我的帖子:
if(typeof(responsetext)=string){responsetext=JSON.parse(responsetext);}$.each(responsetext,function(I,item){disabledDays.push(item);})
@AnhTú感谢您的回复!我尝试了您的解决方案,但结果是一个空列表(控制台中没有错误)。您没有正确使用$.ajax
调用。它返回一个jqXHR对象
,而不是来自URL的响应。您需要做的是在$.ajax
中实现success
函数。更多信息请参见此infohttp://api.jquery.com/jQuery.ajax/
GET http://localhost:8080/disabled-days
200 OK
39ms
jquery-1.9.1.js (regel 8526)
Object { readyState=1, getResponseHeader=function(), getAllResponseHeaders=function(), meer...}
var disabledDays1 = $.ajax({
type: 'GET',
url: "/disabled-days",
});
var disabledDays = []
for (i = 0; i < disabledDays1.length; i++) {
item = results[i];
if(typeof(responsetext)==string){responsetext= JSON.parse(responsetext);} $.each(responsetext,function(i,item){disabledDays.push(item);});
}
console.log(disabledDays);
var disabledDays = []
$.ajax({
type: 'GET',
url: "/disabled-days",
success: function(data){
// data is the response json
disabledDays = data
// do whatever it needs to be done with disabledDays,
// it should be the array of dates.
// the process should be inside this method because of asynchronous
// javascript method execution
}
});