views:

240

answers:

1

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

+2  A: 

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

Bernhard Hofmann
Thanks! That did the trick, now to figure out why Drupal doesn't automatically escape \x26
Angelo R.