需要帮助解决奇怪的 json 解析问题

发布于 2024-10-13 15:42:49 字数 1370 浏览 4 评论 0原文

我在解析从 ajax 调用(使用 jQuery 1.4.4)返回的格式良好的 json 时遇到了奇怪的问题。奇怪的是,在我的开发服务器上它工作正常但在线失败。

ajax 调用返回的数据如下:

returnData = { "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "[email protected]", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } };

jsLint 和 jsonLint 都验证此结构。

当我尝试访问 returnData.data 时发生错误。

在“失败”情况下,我已从 jQuery.ajax 选项中删除了 dataType,从而允许“最佳猜测”功能。如果我指定 json,jQuery 会抛出解析错误,声称 json 无效。我尝试了各种方法(包括可怕的 eval() 和 jquery-2json 插件,但没有运气。甚至 jQ 实用程序 jQuery.parseJSON 也失败了。

这个问题出现在 FF 3.6.13 和最新的 Safari / Chrome 中。

问题 1 :有人知道为什么最新的 jQuery 会抛出解析错误吗?

问题 2:当我尝试以下操作时,我成功了:

  • var success = returnData.status;

但以下内容未定义:

  • var errorReturn = returnData.data.error_return。

奇怪的是,如果我将对象“粘贴”到控制台中,但在脚本中,Firebug 会将其视为一个对象 1)在console.dir中返回“无子对象” 2)但是会在console.log中显示该对象。

非常感谢想法

更新: 我发现服务器错误地设置了内容类型。在格式化 JSON 以返回的服务器端 PHP 中(在 Drupal 6 中创建(在本例中我必须破解核心可选包含“commons.inc”)),我将内容类型替换为“application/json”。现在,这个问题已在 Drupal 7 中得到纠正。

I have odd problems with parsing what I believe to be well-formed json returned from an ajax call (using jQuery 1.4.4). Oddly, on my dev server it works fine but fails online.

The data is returned from the ajax call as follows:

returnData = { "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "[email protected]", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } };

jsLint and jsonLint both validate this structure.

The error occurs when I try to access returnData.data

In the "fail" cases, I have removed the dataType from the jQuery.ajax options, thereby allowing the "best-guess" feature. If I specify json, jQuery throws a parse error, claiming invalid json. I have tried all kinds of things (including the dreaded eval() and the jquery-2json plugin but no luck. Even the jQ utility jQuery.parseJSON fails.

The problem occurs in both FF 3.6.13 and latest Safari / Chrome.

Question 1: does anyone why know the latest jQuery throws a parse error on this?

Question 2: when I try the following, I have success:

  • var success = returnData.status;

BUT the following is undefined:

  • var errorReturn = returnData.data.error_return.

Oddly, Firebug sees this as an object if I "paste" the object into the console, but within the script
1) returns "no child objects" in console.dir
2) BUT will display the object in console.log.

Ideas greatly appreciated

UPDATE:
I discovered that the server was setting the Content type incorrectly. In the server-side PHP that formats the JSON for return (created in this case in Drupal 6 (I had to hack the core-optional include "commons.inc"), I replaced the content type with 'application/json'. This now works. This problem has been corrected in Drupal 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

独留℉清风醉 2024-10-20 15:42:49

如果您引用的文本实际上是返回的内容,并包含 return_data = 部分,那么它是无效的 JSON。

如果您的 ajax 调用如下所示:

$.ajax({
    url: "your_url",
    success: function(data) {

    }
});

...并且在 success 内您想要访问 status 值,您的 JSON 应如下所示:(

{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "[email protected]", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }

注意,没有 return_data = 在开头,没有 ; 在结尾。)

...并且您的 success 函数应该如下所示:

success: function(data) {
    if (data.status) {
        // ...
    }
}

实时示例

该示例适用于最新的 Chrome、Firefox、Opera...,使用最新的 jQuery。也许在某个阶段,您使用的 JSON 解析器在幕后使用了 eval。您引用的示例是作为赋值语句一部分的有效 JavaScript 对象文字表示法,但不是有效的 JSON。一些“JSON”解析器实际上使用 eval 来解析 JavaScript 而不是 JSON,这可能是您的问题。

If your quoted text is actually what's being returned, complete with the return_data = part, then it's invalid JSON.

If your ajax call looks like this:

$.ajax({
    url: "your_url",
    success: function(data) {

    }
});

...and within success you want to access the status value, your JSON should look like this:

{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "[email protected]", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }

(Note, no return_data = at the beginning, no ; at the end.)

...and your success function should look like this:

success: function(data) {
    if (data.status) {
        // ...
    }
}

Live example

That example works with the latest Chrome, Firefox, Opera, ..., using the latest jQuery. Perhaps at some stage you were using a JSON parser that used eval under the covers. Your quoted example is valid JavaScript object literal notation as part of an assignment statement, but not valid JSON. Some "JSON" parsers actually use eval to parse JavaScript rather than JSON, that may have been your issue.

默嘫て 2024-10-20 15:42:49

请参阅我的更新,因为我已经解决了问题。我很抱歉在我原来的帖子中没有更具体。变量 returnedData 实际上位于成功回调中(抱歉没有指定。)

为了清楚起见,我将以下内容与 jQ Submit() 函数一起使用(在此表单提交的点击处理程序中)

var ajaxOptions = {   
  type: 'POST',  
  url: url,   
  dataType: "json",  
  beforeSubmit: checkInput,   
  success:
  evaluateResponse  
  }

  e.preventDefault();

  $(this).ajaxSubmit(contactOptions);

:成功回调是:(

  function
  evaluateContactResponse(returnData) {
  //and so on }

旁注:我一直在使用 jQ 表单插件,直到发生这些失败,但由于表单插件默默失败而转移到不同的实现。我现在可以将其移回,因为表单插件是如此优雅。

)回调“evaluateResponse”是解析发生的地方,正如我所说,现在可以工作了。

再次感谢...你是一个聪明人

See my update as I have solved the problem. I apologize for not being more specific in my original post. The variable returnedData is actually in the success callback (sorry about not specifying.)

For clarity, I am using the following with the jQ submit() function (in the click handler for this form submission):

var ajaxOptions = {   
  type: 'POST',  
  url: url,   
  dataType: "json",  
  beforeSubmit: checkInput,   
  success:
  evaluateResponse  
  }

  e.preventDefault();

  $(this).ajaxSubmit(contactOptions);

Where the success callback is:

  function
  evaluateContactResponse(returnData) {
  //and so on }

(Side-note: I was using the jQ form plugin until these failures occurred but moved to a different implementation since the form plugin failed silently. I may now move this back since the form plugin is so elegant.)

The callback "evaluateResponse" is where the parsing takes place, and as I said, now works.

Thanks again... you are a smart guy
N

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