$.ajax jsonp parsererror

场景重现

通过$.ajax()发起的跨越请求代码如下:

$.ajax({
    dataType: "JSONP",
    type: "GET",
    url: "http://...",
    data: {},
    success: function(data, textStatus){
        // 这里什么都没处理,写了也没反应.
    },
    error: function(xhr, textStatus, ex){
         console.log(textStatus);   
         console.log(ex);
    }        
});

发起 ajax 跨域请求后...

// Firefox 控制台下总是输出
SyntaxError: missing ; before statement

咋一看SyntaxError我还以为是我代码哪里写漏了,检查过后,代码没问题.
同时把完整的请求,直接扔到浏览器地址栏里访问,响应是正常的.
但是在上面的代码中,死活不执行success对应的函数.
同时error对应的函数输出如下:

parsererror
Error: jQuery111209138057178219225_1496905174485 was not called
堆栈跟踪:
error@http://localhost:5903/js/jquery/jquery-1.11.2.min.js:2:1809
b.converters["script json"]@http://localhost:5903/js/jquery/jquery-1.11.2.min.js:4:27729
Pc@http://localhost:5903//js/jquery/jquery-1.11.2.min.js:4:18329
x@http://localhost:5903/js/jquery/jquery-1.11.2.min.js:4:21743
send/b.onreadystatechange@http://localhost:5903/js/jquery/jquery-1.11.2.min.js:4:27017

错误原因

初步怀疑是接口提供方没有处理跨域请求,没有把处理结果包含到JSONP中对应的回调函数中再返回,而是直接返回了 JSON 格式的处理结果.

解决办法

TODO:待我试试通过转发的方式能否解决...

原文地址:https://www.cnblogs.com/taadis/p/12126134.html