jQuery JSON 响应总是触发 ParseError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5095307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:33:59  来源:igfitidea点击:

jQuery JSON response always triggers a ParseError

jqueryjsonplayframework

提问by Chesrae

I am trying to preform some basic operations with jQuery and JSON. Presently having difficulty with jQuery accepting JSON response from my play framework application. Below is a simplified version of the code that still produces the error.

我正在尝试使用 jQuery 和 JSON 执行一些基本操作。目前 jQuery 难以接受来自我的播放框架应用程序的 JSON 响应。下面是仍然产生错误的代码的简化版本。

$.ajax({
    type: 'POST',
    url: "@{FrontEnd.isUsernameAvailable()}",
    data: "name=thisnameisavailable",
    cache: false,
    success: function(data) {
        console.log("Success... ");
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("Error... " + textStatus + "        " + errorThrown);
    },
    dataType: 'json'
});

The error callback is always triggered. It displays

错误回调总是被触发。它显示

Error... parsererror jQuery15001997238997904205_1298484897373 was not called

错误... parsererror jQuery15001997238997904205_1298484897373 未被调用

Inspecting the returned JSON through Firebug shows no errors and various JSON lint tools also validate. Changing dataType to "text" makes success be called. But I am trying to use the isUsernameAvailable call as part of jQuery validation plugin so I need it to return valid JSON.

通过 Firebug 检查返回的 JSON 没有显示任何错误,并且各种 JSON lint 工具也进行了验证。将 dataType 更改为“text”会调用成功。但我试图使用 isUsernameAvailable 调用作为 jQuery 验证插件的一部分,所以我需要它返回有效的 JSON。

采纳答案by ggutenberg

Maybe I'm misunderstanding, but couldn't you set the dataTypeto textand JSON.parse() the returned data?

也许我误解了,但是你不能设置dataTypetotext和 JSON.parse() 返回的数据吗?

success: function(data) {
    data = JSON.parse(data);
    // process data
},

Edited to add generally agreed upon solution (previously a comment only):

编辑添加普遍同意的解决方案(以前仅评论):

I just took a look at api.jquery.com/jQuery.ajaxand it looks like with jQuery 1.5 you can do a type conversion of sorts. "multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType." Maybe you can try "text json".

我刚刚查看了api.jquery.com/jQuery.ajax,看起来使用 jQuery 1.5 可以进行各种类型的转换。“多个空格分隔的值:从 jQuery 1.5 开始,jQuery 可以将数据类型从它在 Content-Type 标头中收到的内容转换为您需要的内容。例如,如果您希望将文本响应视为 XML,请使用“文本 xml" 用于数据类型。" 也许您可以尝试“文本 json”。

回答by Johnny Oshika

I got the same error as soon as I upgraded to jQuery 1.5. It turns out that my problem is because I'm also using the jquery validation plugin, which is not compatible with jQuery 1.5. If I remove the jquery validation plugin, $.ajax() with dataType json works fine.

升级到 jQuery 1.5 后,我立即遇到了同样的错误。事实证明,我的问题是因为我也在使用与 jQuery 1.5 不兼容的 jquery 验证插件。如果我删除了 jquery 验证插件,带有 dataType json 的 $.ajax() 工作正常。

More information about the jquery validation plugin incompatibility here: http://bugs.jquery.com/ticket/8118

有关 jquery 验证插件不兼容性的更多信息,请访问:http: //bugs.jquery.com/ticket/8118

回答by Robert

I also got "parsererror jQueryNNNN_NNN was not called" (using jsonp and jQuery 1.7.2) The reason was that one of the values in the returned json structure contained newlines. Hope this helps someone.

我还收到“未调用解析器错误 jQueryNNNN_NNN”(使用 jsonp 和 jQuery 1.7.2)原因是返回的 json 结构中的值之一包含换行符。希望这可以帮助某人。

回答by Abdull

I got parseerror, because urlcontained a callback=?part. This is a magic string which activates JSONP functionality.

我得到了parseerror,因为url包含了callback=?一部分。这是一个激活 JSONP 功能的魔法字符串

As my server side's REST API changed from JSONP to JSON, the data format returned from it was no longer compatible with jQuery.getJSON(..)with callback=?. In this situation, jQuery.getJSON(..)won't call the successcallbacks, but the failcallbacks instead.

由于我的服务器端的 REST API 从 JSONP 更改为 JSON,从它返回的数据格式不再jQuery.getJSON(..)callback=?. 在这种情况下,jQuery.getJSON(..)不会调用success回调,而是fail调用回调。

I resolved this issue by removing the callback=?part from the urlparameter.

我通过callback=?url参数中删除部分解决了这个问题。