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": "t@t.com", "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.
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": "t@t.com", "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.
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
精彩评论