Skip to content Skip to sidebar Skip to footer

With JQuery, Access Json From Cross Domain Url Where Json May Be Poorly Formed

I'm using jQuery's ajax function to hit a cross domain url. The url will return json. I'm in the discovery phase with this but I think, because the json values contain several '&am

Solution 1:

The problem is the JSONP service, it is dismissing completely the callback parameter.

A JSONP response is simply a function call using the callback get parameter as the function name:

If you look the response of some JSONP service, it should be like this:

http://somesite.com/jsonp?callback=myCallback

Would return:

myCallback({/*json*/});

While the one you post is returning plain valid JSON, but it cannot be handled as a JSONP response.


Solution 2:

Hmm. I don't know jQuery, but I know JSON. (I wrote a C++ based parser for it.) I went to the url you show: http://www.codemash.org/rest/sessions.json?format=jsonp&callback=?. Python's JSON parser accepts that, and my experience is that Python's JSON parser is correct. Note that this:

{ "blah": "string " More more more" }

...is valid JSON. The " part gets no special interpretation in JSON - they are just characters in the string.


Edit: Looking harder at this, I find it very interesting that whatever is generating this is escaping some forward slashes, but not all. The "Start" items are "/Date(-XXXXX)/", which is the text /Date(-XXXXX)/ - / in JSON can optionally be escaped. However, in items with keys "SpeakerURI" and "URI", they are not escaped.


Edit: Ran this through my C++ based parser. Initially it did not take it, but this JSON actually exposed a bug in my parser.


Solution 3:

You might consider trying the jquery-jsponp plugin, which fixes some problems with jQuery's jsonp handling.

http://code.google.com/p/jquery-jsonp/

I use it and there are a lot of great examples.

You really need to get your callbacks working so you can see what's going on.


Update: I took a stab with jsonp and I'm getting unexpected token errors. Not sure why--I can grab the data manually from the URL, assign it to a JavaScript variable in the console, and it's just fine.


Solution 4:

After analyzing your data on an hour,

I found following

{
   "URI":"/rest/sessions/Maintainable-ASPNET-MVC",
   "Title":"Maintainable ASP.NET MVC",
   "Abstract":".....\u003e\u2028Duration: 60-90 minutes\u003cbr\u003e\u2028Subject Area: development....."
}

There is char \u2028 before Duration, and Subject Area,

\u2028 is line seperator, so Its mean, kind of \n, so you have line break between the quotes "", that may be root case.

If you remove that, You wont see unterminated string literal in firebug.

To test that, paste following in address bar, Firefox and Opera fails, while IE8 and Chrome is working

javascript:alert(eval('[{"Abstract":"\u003e
    Duration: 60-90 minutes\u003cbr\u003e
    Subject Area: development."}]'))

somebody blogged about that already

http://bulknews.typepad.com/blog/2009/02/json-jquery-and-some-unicode-characters-u2028.html


Solution 5:

I don't appear to be any closer to a client side solution. I changed the code to use jQuery-jsonp. It now looks like this:

$(function() {
  $.jsonp({
  url: "http://www.codemash.org/rest/sessions.json?format=jsonp&callback=?", 
  async: false,
  dataType: "jsonp",
  success: successFunc,
  complete: completeFunc,
  error: errorFunc,
  dataFilter: dataFilterFunc
});

});

function successFunc(json, textStatus) { 
  console.log('successFunc(). enter.'); 
}
function completeFunc(xOptions, textStatus) { 
  console.log('complete(). textStatus: ['+textStatus + ']'); 
}
function errorFunc(xOptions, textStatus) { 
  console.log('errorFunc(). textStatus: [' + textStatus + ']'); 
}
function dataFilterFunc(j) { 
  console.log('dataFilterFunc(). enter.');
  return j; 
}

Firebug's console log displays:

X unterminated string literal
[{"URI"..... ( the json returned from the server
errorFunc(). textStatus: [error]
complete(). textStatus: [error]

For whatever reason, the json parse in my Firefox browser (FF v3.0.15, Linux Ubuntu 9.0.4) is throwing the "unterminated string literal" exception. I know if I remove the '& q u o t' strings and two characters in the 'Maintainable ASP.NET MVC' block then the json will parse successfully. Since I have no control on the server side, is there any thing I can do with client-side javascript that will eventually get the server's json string, as-is, into a client side json object. I thought the dataFilter property for jsonp was set up exactly for that purpose, to intercept the json and 'scrub' it. However, that function in my code is not called, quite possibly because the json that is passed into the dataFilterFunc function will not parse successfully.


Post a Comment for "With JQuery, Access Json From Cross Domain Url Where Json May Be Poorly Formed"