我遇到了这种错误:
SyntaxError: missing ; before statement
我不知道是什么原因导致了错误,但我这里有这段代码:
(function pollschedule(){
$.ajax({type: "GET",
dataType: "jsonp",
contentType: "application/json",
url: "http://127.0.0.1:8080/get/schedule/1/",
success: function(data){
console.log(data);
},
complete: pollschedule, timeout: 5000});
})();
如果您可能需要查看 http://127.0.0.1:8080/get/schedule/1/是:
def get_schedule(request, sid):
schedule = Schedule.objects.filter(id=sid, date=datetime.datetime.now()).values('id', 'sched__name', 'date', 'time')
sched_collection = collections.defaultdict(list)
for i in schedule:
sched_collection[i['sched__name']].append(i)
return JsonResponse({"schedule" : dict(sched_collection)})
并返回:
{"schedule": {"CWW": [{"date": "2016-11-11", "time": "17:10:10"}]}}
错误指向响应中“schedule”后面的分号。
最佳答案
JSONP不是JSON 。您的响应是 JSON,但您已告诉 jQuery 需要 JSONP。
要么使响应有效 JSONP,要么更新执行检索的代码以期望 JSON。 (请注意,如果您执行后者,您将遇到跨域问题,除非您运行 ajax 的页面也在 http://127.0.0.1:8080
上。)
关于jquery - 语法错误: missing ; before statement on JSONResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40543800/