2

I grab a JSON string from an API using jQuery's dataType 'jsonp'. The operation fails because the returning obj contains strings which have ':' colons in it.

Find fiddle here: http://jsfiddle.net/ezmilhouse/vZjV4/1/

var url = 'http://api.spreadshirt.com/api/v1/shops/329852/articles?fullData=true&locale=us_US&offset=0&attributeSet=staticShop&mediaType=jsonp';

$.ajax({
  cache: false,
  callback: "callback",
  dataType: 'jsonp',
  pageCache: false,
  url: url,
  callbackParameter: "callback",
  success: function(data, status, jqXHR) {
    console.log(data);  
}});

The returning obj looks like this, which is valid JSON (tested with jsonlint.com) :

{"articles": [
    {
        "name": "Honoring Generations of Mothers - Youth TShirt",
        "description": "t-shirt for women, Brand: ALO"
    }
]}

But jQuery throws a

unterminated string literal

error because it doesn't like the colon after 'Brand'

"description": "t-shirt for women, Brand: ALO" // colon causes error

Do colons need to be escaped to work with jQuery's jsonp? Any workarounds?

thx

2
  • in theory they shouldn't have to be escaped, because json treats the description value as a string, but if it helps, try and escape it or use a better json encode/decode-r Commented Nov 8, 2011 at 11:28
  • json.parser.online.fr gives me no error ... Commented Nov 8, 2011 at 11:38

1 Answer 1

2

I got your exception, but reason is not colon, your jsonp response is malformed (it is very large, about 38k characters, and seems to me that it is limited to 64k or something like that, so it is missing the end).

It ends with: "size":{"id":"2"},"

" at the end is unterminated string literal

If you would close it (i.e. "size":{"id":"2"}}]};) only then browser would be able to parse that jsonp.

EDIT

I can get whole response from chrome, but its dev tools is showing only part of it, I was wrong. Will check it in IE.

EDIT2

If you inspect your response in IE developer tools, you will see that you have newline in jsonp twice, every time after Brand: American Apparel, so it breaks the string and js cannot parse jsonp.

Sign up to request clarification or add additional context in comments.

4 Comments

i guess the json-object is somewhat to big (size) for jquery ... i've searched the docu for $.ajax and its settings, but did not find any corresponding one...
what do you mean by 'newline twice'?
at two places in jsonp response: first time after 'American Apparel' on char 81633 and second time after 'American Apparel' on char 90656. Remove (convert to \r\n) these and try again.
EDIT 2 is the correct answer, there are invalid control characters ("\r") in the payload that break the parser. The specs are very clear about that: json.org (right column), as a workaround notice that the specification provides a "\n" escape for newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.