问题
The call looks something like so
$.ajax({
url: url,
crossDomain: true,
dataType: 'jsonp',
success: function(){console.log('success');}
})
From the server code it something like so
string data = callback + "(" jsonData + ")";
send("application/javascript",data);
The jsonData is valid, which I'm pretty sure of. I have tested it out on many json validators online. I've also tried switching the content-type from application/javascript to application/json in the server code but it does no difference.
The only way I've got this to work is override the callback function sent my jquery, and have it so that instead of calling jquery success function, it calls my own global function. And here is the code for that workaround
$.ajax({
url: url,
crossDomain: true,
dataType: "jsonp",
jsonpCallback: "onMyDataReceived"
})
function onMyDataReceived(jsonData){ doStuff(jsonData ); }
but I feel like this is more of a hack instead of having it naturally flow from $.ajax.sucess function. Any help on why the first code snippet would error out?
回答1:
I am really not sure why the first snippet didn't work, but trying the following
$.getJSON(url+"&callback=?",function(data){ console.log(data);})
worked in my case.
来源:https://stackoverflow.com/questions/13353871/i-am-getting-a-json-parse-error-on-a-cross-domain-ajax-call-not-sure-how-to-get