javascript - Jquery ajax json 不執行success的原因

來源:互聯網
上載者:User
關鍵字 php javascript html
 $.ajax({            url: 'XXXX.com/index.php/Home/Shop/transCloseShop',            type: 'GET',            dataType: 'jsonp',            timeout: 1000,            cache: false,            data:{                robot:"123",                usertype:"0",                userid:"22",                shopid:"4",                transToCondition:1,            },            error: erryFunction,  //錯誤執行方法            success: succFunction //成功執行方法        })        function erryFunction(data) {            console.log(data);        }        function succFunction(){            alert("22");        }

現在執行的是error方法

看到網上的資料

隨著Jquery版本的提高,對於json格式有著嚴格的要求,原先類似這種格式的返回資訊{success:true,id:1,result:"ok"}在高版本中已不適用,需採取以下格式 改成{"success":true,"id":"1"} 就可以正常執行success回調了。

但是我下面就是嚴格的json格式啊

下面是伺服器輸出的json

[{"shop_id":"3","shop_name":"\u65b0\u4e16\u754c\u4e2d\u9910","shop_bossid":"22","shop_history_bossid":"-22","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460110593","shop_updatetime":"1460110593","opentime":"46\u592915\u5c0f\u65f645\u5206\u949f12\u79d2","historyboss":";13881119614;"},{"shop_id":"4","shop_name":"\u4e2d\u4e1c\u4e16\u754c","shop_bossid":"22","shop_history_bossid":"-18-15","shop_condition":"1","shop_isdelete":"0","shop_createtime":"1460111243","shop_updatetime":"1460111243","opentime":"46\u592915\u5c0f\u65f634\u5206\u949f22\u79d2","historyboss":";;;"},{"shop_id":"5","shop_name":"\u80af\u6253\u9e21","shop_bossid":"22","shop_history_bossid":"","shop_condition":"1","shop_isdelete":"0","shop_createtime":"1460116195","shop_updatetime":"1460116195","opentime":"46\u592914\u5c0f\u65f611\u5206\u949f51\u79d2","historyboss":""},{"shop_id":"6","shop_name":"\u54c8\u6839\u8fbe\u65af","shop_bossid":"22","shop_history_bossid":"-22-17","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460116659","shop_updatetime":"1460116659","opentime":"46\u592914\u5c0f\u65f64\u5206\u949f7\u79d2","historyboss":";13881119614;;"},{"shop_id":"12","shop_name":"\u5b9c\u5bbe\u71c3\u9762","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460378016","shop_updatetime":"1460378016","opentime":"43\u592913\u5c0f\u65f628\u5206\u949f10\u79d2","historyboss":""},{"shop_id":"13","shop_name":"\u78e8\u83dc\u5200","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460421729","shop_updatetime":"1460421729","opentime":"43\u59291\u5c0f\u65f619\u5206\u949f37\u79d2","historyboss":""},{"shop_id":"26","shop_name":"\u7f8a\u7ecd\u6b66","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462841544","shop_updatetime":"1462841544","opentime":"15\u59291\u5c0f\u65f69\u5206\u949f22\u79d2","historyboss":""},{"shop_id":"27","shop_name":"\u4e1c\u62c9\u897f\u626f","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462842809","shop_updatetime":"1462842809","opentime":"15\u59290\u5c0f\u65f648\u5206\u949f17\u79d2","historyboss":""},{"shop_id":"29","shop_name":"13123","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462955116","shop_updatetime":"1462955116","opentime":"13\u592917\u5c0f\u65f636\u5206\u949f30\u79d2","historyboss":""},{"shop_id":"30","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276283","shop_updatetime":"1463276283","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f43\u79d2","historyboss":""},{"shop_id":"31","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276286","shop_updatetime":"1463276286","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f40\u79d2","historyboss":""},{"shop_id":"32","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276289","shop_updatetime":"1463276289","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f37\u79d2","historyboss":""},{"shop_id":"33","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546341","shop_updatetime":"1463546341","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f45\u79d2","historyboss":""},{"shop_id":"34","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546342","shop_updatetime":"1463546342","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f44\u79d2","historyboss":""},{"shop_id":"37","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546345","shop_updatetime":"1463546345","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f41\u79d2","historyboss":""},{"shop_id":"39","shop_name":"","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546346","shop_updatetime":"1463546346","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f40\u79d2","historyboss":""},{"shop_id":"40","shop_name":"","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546347","shop_updatetime":"1463546347","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f39\u79d2","historyboss":""}]

回複內容:

 $.ajax({            url: 'XXXX.com/index.php/Home/Shop/transCloseShop',            type: 'GET',            dataType: 'jsonp',            timeout: 1000,            cache: false,            data:{                robot:"123",                usertype:"0",                userid:"22",                shopid:"4",                transToCondition:1,            },            error: erryFunction,  //錯誤執行方法            success: succFunction //成功執行方法        })        function erryFunction(data) {            console.log(data);        }        function succFunction(){            alert("22");        }

現在執行的是error方法

看到網上的資料

隨著Jquery版本的提高,對於json格式有著嚴格的要求,原先類似這種格式的返回資訊{success:true,id:1,result:"ok"}在高版本中已不適用,需採取以下格式 改成{"success":true,"id":"1"} 就可以正常執行success回調了。

但是我下面就是嚴格的json格式啊

下面是伺服器輸出的json

[{"shop_id":"3","shop_name":"\u65b0\u4e16\u754c\u4e2d\u9910","shop_bossid":"22","shop_history_bossid":"-22","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460110593","shop_updatetime":"1460110593","opentime":"46\u592915\u5c0f\u65f645\u5206\u949f12\u79d2","historyboss":";13881119614;"},{"shop_id":"4","shop_name":"\u4e2d\u4e1c\u4e16\u754c","shop_bossid":"22","shop_history_bossid":"-18-15","shop_condition":"1","shop_isdelete":"0","shop_createtime":"1460111243","shop_updatetime":"1460111243","opentime":"46\u592915\u5c0f\u65f634\u5206\u949f22\u79d2","historyboss":";;;"},{"shop_id":"5","shop_name":"\u80af\u6253\u9e21","shop_bossid":"22","shop_history_bossid":"","shop_condition":"1","shop_isdelete":"0","shop_createtime":"1460116195","shop_updatetime":"1460116195","opentime":"46\u592914\u5c0f\u65f611\u5206\u949f51\u79d2","historyboss":""},{"shop_id":"6","shop_name":"\u54c8\u6839\u8fbe\u65af","shop_bossid":"22","shop_history_bossid":"-22-17","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460116659","shop_updatetime":"1460116659","opentime":"46\u592914\u5c0f\u65f64\u5206\u949f7\u79d2","historyboss":";13881119614;;"},{"shop_id":"12","shop_name":"\u5b9c\u5bbe\u71c3\u9762","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460378016","shop_updatetime":"1460378016","opentime":"43\u592913\u5c0f\u65f628\u5206\u949f10\u79d2","historyboss":""},{"shop_id":"13","shop_name":"\u78e8\u83dc\u5200","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1460421729","shop_updatetime":"1460421729","opentime":"43\u59291\u5c0f\u65f619\u5206\u949f37\u79d2","historyboss":""},{"shop_id":"26","shop_name":"\u7f8a\u7ecd\u6b66","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462841544","shop_updatetime":"1462841544","opentime":"15\u59291\u5c0f\u65f69\u5206\u949f22\u79d2","historyboss":""},{"shop_id":"27","shop_name":"\u4e1c\u62c9\u897f\u626f","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462842809","shop_updatetime":"1462842809","opentime":"15\u59290\u5c0f\u65f648\u5206\u949f17\u79d2","historyboss":""},{"shop_id":"29","shop_name":"13123","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1462955116","shop_updatetime":"1462955116","opentime":"13\u592917\u5c0f\u65f636\u5206\u949f30\u79d2","historyboss":""},{"shop_id":"30","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276283","shop_updatetime":"1463276283","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f43\u79d2","historyboss":""},{"shop_id":"31","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276286","shop_updatetime":"1463276286","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f40\u79d2","historyboss":""},{"shop_id":"32","shop_name":"\u9999\u5929\u4e0b\u706b\u9505","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463276289","shop_updatetime":"1463276289","opentime":"10\u59290\u5c0f\u65f623\u5206\u949f37\u79d2","historyboss":""},{"shop_id":"33","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546341","shop_updatetime":"1463546341","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f45\u79d2","historyboss":""},{"shop_id":"34","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546342","shop_updatetime":"1463546342","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f44\u79d2","historyboss":""},{"shop_id":"37","shop_name":"\u8d85\u5e02\u5b9d","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546345","shop_updatetime":"1463546345","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f41\u79d2","historyboss":""},{"shop_id":"39","shop_name":"","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546346","shop_updatetime":"1463546346","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f40\u79d2","historyboss":""},{"shop_id":"40","shop_name":"","shop_bossid":"22","shop_history_bossid":"","shop_condition":"0","shop_isdelete":"0","shop_createtime":"1463546347","shop_updatetime":"1463546347","opentime":"6\u592921\u5c0f\u65f622\u5206\u949f39\u79d2","historyboss":""}]

jsonp請求有特殊要求,你的代碼應該類似:

$.ajax({    url: 'XXXX.com/index.php/Home/Shop/transCloseShop',    type: 'GET',    dataType: 'jsonp',    timeout: 1000,    cache: false,    jsonp: false,    data:{        robot:"123",        usertype:"0",        userid:"22",        shopid:"4",        transToCondition:1,    },    jsonpCallback: 'successCallback'})function successCallback(data){    console.log('成功!');}

然後伺服器端返回的內容不能是一個純json,得是一個可執行檔指令碼,根據上面我們設定的jsonpCallback,伺服器返回的內容應該大致這個樣子:

successCallback([{"shop_id":"3","shop_name":"\u65b0\u4e16\u754c\u4e2d\u9910"...}]);

問題出在了這裡 dataType : 'jsonp'。我將類型改為了json就調用success方法了。
你返回的資料看起來僅僅是一個普通的json資料.

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.