4
我试图让一个JSONP请求允许跨域。的情况是,我的服务器不处理JSONP请求,并理解这个请求作为JSON和响应背部采用了JSON对象:{result: 1}
当dataType设置为JSONP时读取JSON响应
这里是我的Ajax请求:从窗口控制台
jQuery.ajax({
type : 'POST',
url : "https://example.com/addUser",
data : {
firstName : 'firstName',
lastName : 'lastName',
email : '[email protected]'
},
crossDomain : true,
jsonpCallback: "jsonpCallback",
success : function(data) {
alert(data);
},
complete : function(jqXHR, textStatus) {
//can do something here
},
error : function (xhr, textStatus, errorThrown) {
if(errorThrown=="jsonpCallback was not called") {
console.log('caught it!');
}
},
dataType : 'jsonp'
});
读物:
Resource interpreted as Script but transferred with MIME type text/json: "https://example.com/addUser…Name=firstName&lastName=lastName&email=email%40yopmail.com&_=1359646137496".
Uncaught SyntaxError: Unexpected token :
caught it!
正如预期的那样,它会抛出异常parseerror,我尝试来处理它。但我的问题是,由于浏览器实际上得到了回应{result:1}
,有没有办法我可以解析它?
难道ü尝试改变像数据:'VAR jsonBuilder = JSON.stringify(数据:{ 姓: '的firstName', 名字: 'lastName的', 电子邮件:'[email protected]' });'? –
如果服务器不支持JSONP,则无法向其发出JSONP请求。故事结局。唯一的方法是通过服务器上的脚本代理请求。 –
如果您尝试在'complete'方法中'console.log'或'error'方法中的'xhr.responseText'中的'jqXHR.responseText',会发生什么? – Ian