ホームページ  >  記事  >  バックエンド開発  >  javascript - Jquery ajax json 不执行success的原因

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

WBOY
WBOYオリジナル
2016-06-06 20:07:46975ブラウズ

<code> $.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");
        }</code>

现在执行的是error方法

看到网上的资料

随着Jquery版本的提高,对于json格式有着严格的要求,原先类似这种格式的返回信息{success:true,id:1,result:"ok"}在高版本中已不适用,需采取以下格式 改成{"success":true,"id":"1"} 就可以正常执行success回调了。

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

下面是服务器输出的json

<code>[{"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":""}]</code>

回复内容:

<code> $.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");
        }</code>

现在执行的是error方法

看到网上的资料

随着Jquery版本的提高,对于json格式有着严格的要求,原先类似这种格式的返回信息{success:true,id:1,result:"ok"}在高版本中已不适用,需采取以下格式 改成{"success":true,"id":"1"} 就可以正常执行success回调了。

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

下面是服务器输出的json

<code>[{"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":""}]</code>

jsonp请求有特殊要求,你的代码应该类似:

<code class="javascript">$.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('成功!');
}</code>

然后服务器端返回的内容不能是一个纯json,得是一个可执行的脚本,根据上面我们设置的jsonpCallback,服务器返回的内容应该大致这个样子:

<code class="javascript">successCallback([{"shop_id":"3","shop_name":"\u65b0\u4e16\u754c\u4e2d\u9910"...}]);</code>

问题出在了这里 dataType : 'jsonp'。我将类型改为了json就调用success方法了。
你返回的数据看起来仅仅是一个普通的json数据.

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。