I am getting a json parse error on a cross-domain ajax call not sure how to get rid of the issue

倾然丶 夕夏残阳落幕 提交于 2019-12-12 02:23:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!