zoukankan      html  css  js  c++  java
  • $.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:待我试试通过转发的方式能否解决...

  • 相关阅读:
    bzoj1467 Pku3243 clever Y
    bzoj2242 [SDOI2011]计算器
    卡特兰数
    洛谷P1290 欧几里得的游戏
    bzoj2277 [Poi2011]Strongbox
    poj2406 Power Strings
    Codeforces 892 D.Gluttony
    Codeforces 892 C.Pride
    Codeforces 892 B.Wrath
    Codeforces 892 A.Greed
  • 原文地址:https://www.cnblogs.com/taadis/p/12126134.html
Copyright © 2011-2022 走看看