为什么即使 $.getJSON 运行正常,警报也没有运行? (回调未执行,即使请求使用 getJSON 工作正常)
我有一段代码,例如:
$.getJSON("http://mysite.org/polls/saveLanguageTest?url=" + escape(window.location.href) + "&callback=?",
function (data) {
var serverResponse = data.result;
console.log(serverResponse);
alert(serverResponse);
});
它工作正常,因为它向我的服务器发出跨域请求,并且服务器按我的预期保存数据。不幸的是,即使服务器保存数据并发回响应,我也无法收到任何警报或 console.log 运行。为什么会这样呢?服务器端代码是(如果相关):
def saveLanguageTest(request):
callback = request.GET.get('callback', '')
person = Person(firstName = 'Anonymous',
ipAddress = request.META['REMOTE_ADDR'])
person.save()
webPage = WebPage(url = request.GET.get('url'))
webPage.save()
langTest = LanguageTest(type = 'prepositionTest')
langTest.person = person
langTest.webPage = webPage
langTest.save()
req ['result'] = 'Your test is saved.'
response = json.dumps(req)
response = callback + '(' + response + ');'
return HttpResponse(response, mimetype = "application/json")
我缺少什么? (我在网页和 Firebug 中尝试了相同的代码,但总是遇到上述问题。)
I have a snippet of code such as:
$.getJSON("http://mysite.org/polls/saveLanguageTest?url=" + escape(window.location.href) + "&callback=?",
function (data) {
var serverResponse = data.result;
console.log(serverResponse);
alert(serverResponse);
});
It works fine in the sense that it makes a cross-domain request to my server and the server saves the data as I expect. Unfortunately, even though the server saves data and sends back a response I just can't get any alert or the console.log run. Why may be that? The server side code is (if that is relevant):
def saveLanguageTest(request):
callback = request.GET.get('callback', '')
person = Person(firstName = 'Anonymous',
ipAddress = request.META['REMOTE_ADDR'])
person.save()
webPage = WebPage(url = request.GET.get('url'))
webPage.save()
langTest = LanguageTest(type = 'prepositionTest')
langTest.person = person
langTest.webPage = webPage
langTest.save()
req ['result'] = 'Your test is saved.'
response = json.dumps(req)
response = callback + '(' + response + ');'
return HttpResponse(response, mimetype = "application/json")
What am I missing? (I tried the same code both within my web pages and inside the Firebug and I always have the problem stated above.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$.getJSON
在收到格式错误的 JSON 时会默默地失败。检查您的 JSON 格式是否正确,或者尝试使用简单的一小部分 JSON 来使其正常工作。从 手册:$.getJSON
likes to fail silently when it received malformed JSON. Check that your JSON is well-formed, or try with a simple tiny bit of JSON to get it working. From the manual:您确定您的 Django 代码正在返回,并且没有引发异常吗?据我所知,您那里有无效代码 - 您引用了
req['result']
而没有先定义req
。正如评论中所建议的,查看 Firebug 的控制台选项卡以查看您的调用实际返回的内容。你可能会发现它实际上是一个 Django 错误页面。
Are you sure that your Django code is returning, and not raising an exception? As far as I can see, you have invalid code there - you reference
req['result']
without first definingreq
.As suggested in the comments, look at Firebug's Console tab to see what is actually being returned from your call. You may find it's actually a Django error page.
忘记添加了
我的错是我之前
(我这边的另一个失败是为回调实现错误处理函数,如果 jQuery 的默认 JSONP 功能可能的话)。
My bad' I've forgotten to add
before
(And another failure on my side was to implement an error handling function for the callback, if that's possible at all for jQuery's default JSONP functionality).