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 '"' 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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With