Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '&quot' strings, the json eval is throwing an error and stopping the execution of my client side script. The error that I get is "unterminated string literal". I know that if I hard-code the returned json by putting it in a local div and remove the '"' strings (and a couple of hidden special characters), I can get the json to eval successfully. Whether through work-around, or through a correction to my code, what can I do to get the json from the url and store it in my client side javascript as a valid json object?

my current code. none of the defined functions (success, complete, error, dataFilter) execute:

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

});

function successFunc() { console.log('successFunc(). enter.'); }
function completeFunc() { console.log('complete(). enter.'); }
function errorFunc() { console.log('errorFunc(). enter.'); }
function dataFilterFunc(data, type) { 
  data.replace(/\W/g, ' ');
  return data; 
}
like image 573
mark Avatar asked Oct 14 '22 12:10

mark


1 Answers

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.

like image 151
Christian C. Salvadó Avatar answered Nov 02 '22 20:11

Christian C. Salvadó