gpt4 book ai didi

jquery - 为什么即使 $.getJSON 运行正常,警报也没有运行? (回调未执行,即使请求使用 getJSON 工作正常)

转载 作者:行者123 更新时间:2023-12-01 07:01:55 25 4
gpt4 key购买 nike

我有一段代码,例如:

$.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 中尝试了相同的代码,但总是遇到上述问题。)

最佳答案

$.getJSON 在收到格式错误的 JSON 时喜欢默默地失败。检查您的 JSON 格式是否正确,或者尝试使用简单的一小部分 JSON 来使其正常工作。来自 manual :

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

关于jquery - 为什么即使 $.getJSON 运行正常,警报也没有运行? (回调未执行,即使请求使用 getJSON 工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2780208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com