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

发布于 2024-08-31 08:31:54 字数 1098 浏览 0 评论 0原文

我有一段代码,例如:

$.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热血少△年 2024-09-07 08:31:54

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

重要:从 jQuery 1.4 开始,如果 JSON 文件包含语法错误,
请求通常会失败
默默。避免频繁的手工编辑
由于这个原因,JSON 数据。 JSON 是
具有语法的数据交换格式
比以下规则更严格的规则
JavaScript 的对象字面量表示法。
例如,所有字符串表示
在 JSON 中,它们是否是属性
或值,必须包含在
双引号。有关 JSON 的详细信息
格式,请参见 http://json.org/

$.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:

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/.

歌入人心 2024-09-07 08:31:54

您确定您的 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 defining req.

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.

余生共白头 2024-09-07 08:31:54

忘记添加了

req = {}

我的错是我之前

req ['result'] = 'Your test is saved.'

(我这边的另一个失败是为回调实现错误处理函数,如果 jQuery 的默认 JSONP 功能可能的话)。

My bad' I've forgotten to add

req = {}

before

req ['result'] = 'Your test is saved.'

(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文