Reputation: 2341
I have jQuery.ajax() creating a request to a url (cms2/docman/dir/%id) (%id is a numeric unsigned integer). The page requested runs some functions and outputs an array. This array is then run through drupal_json. drupal_json() echo's out the content first setting the header to
Content-Type: text/javascript; charset=utf-8
So far, everything seems to be going well. The functions are all running and the JSON is being outputted as expected. Through firebug it shows that the response received is JSON and offers the "JSON" tab to preview it.
However, jQuerys jQuery.ajax() function says that a parser error occurred and that it returned "invalid" json. I copied out the json returned and tossed it into an editor (Eclipse PDT) but it shows that there are no errors.
I'm completely stumped at this point. The only thing I can think of is if there is some kind of limit on the amount of text returned via this method.
Creates the request:
function request(url) { $.ajax({ url: url, type: 'POST', dataType: 'json', async: false, success: function(data) { if(data.status) { docman.store = data.info; } else { docman.hideMessages(); docman.error(data.message); } }, error: function(data,ts,et) { docman.hideMessages(); docman.error(data); docman.store = data.responseText; } }); }
JSON output here - http://codetidy.com/102
Upvotes: 0
Views: 2953
Reputation: 3450
you have to do something like this
diff --git a/includes/common.inc b/includes/common.inc
index b86f2d2..ff246a3 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2539,8 +2539,8 @@ function drupal_to_js($var) {
case 'resource':
case 'string':
return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
- array('\r', '\n', '\x3c', '\x3e', '\x26'),
- addslashes($var)) .'"';
+ array('\r', '\n', '\u003c', '\u003e', '\u0026'),
+ str_replace("\'","'", addslashes($var))) .'"';
case 'array':
// Arrays in JSON can't be associative. If the array is empty or if it
// has sequential whole number keys starting with 0, it's not associative
Upvotes: 1
Reputation: 10411
You can use http://www.jsonlint.com/ to check your JSON. You'll find it tells you that line 136 contains an invalid code:
syntax error, unexpected TINVALID at line 136 Parsing failed
You need to double escape the character code. (Two backslashes).
Upvotes: 3