如果3个Ajax返回中只有2个是Json,则JQuery不起作用


JQuery not working if only 2 of 3 Ajax returns are Json

我遇到以下问题。

我写了一个ajax请求,我的PHP文件进行了一些检查,并返回一个数组,我用json_encode()回显该数组或一个普通回显。这取决于我的PHP文件的检查结果。我的问题如下:

当我给Ajax请求dataType: "json"时,似乎只有当我从PHP文件中返回一些json_encode($array)时,它才起作用。如果我回显正常数据,Jquery将不起作用。

一个小例子:

$.ajax(
            {
                type: "get",
                dataType: "json",
                url: "check_basket.php",
                data: {bought : bought, pid : pid, cid : cid, csid : csid, lang : lang},
                success: function(data){    
                            if(data.status == '1'){
                                alert(data.b);
                            } else if(data.status  == '2'){
                                alert(data.b);
                            } else {
                                 $("#dropdown_shopping_cart_inner").html(data);
                                 $('#head_shopping_cart').load(document.URL +  ' #head_shopping_cart');
                                 $('#dropdown_shopping_cart').slideDown(800);
                            }
                }
            }
        );

在我的PHP中,我这样说:

if(get_date_last_purchased($pid) == true){
    $arr = array('status' => 1, 'b' => 'test1');
    echo json_encode($arr);
}
elseif($bought > $row0['gm_max_order']){
    $arr = array('status' => 2, 'b' => 'test2');
    echo json_encode($arr);
} else {
    echo "everything ok";
}

如果前两种情况中有一种发生,并且状态1或2返回到我的JQuery,那么一切都很好。但是,当第三个PHP条件发生时,我的PHP只返回没有json_encode的echo,Jquery就什么都不做了。

有什么建议吗?

在您的示例中,您显式地请求一个json。

尝试较短的表示法,以便能够使用任一数据类型的

$.get("check_basket.php",function(data){
//Do whatever you want to do with your data
}

但你的问题是,你把不同的类型混合在一起。。。任何端点都应该只返回相同的数据类型和格式,所以我建议oyu更改您的PHP

if(get_date_last_purchased($pid) == true){
    $arr = array('status' => 1, 'b' => 'test1');
    echo json_encode($arr);
}
elseif($bought > $row0['gm_max_order']){
    $arr = array('status' => 2, 'b' => 'test1');
    echo json_encode($arr);
} else {
    echo json_encode(array('status'=>0));
}

并像这样更改您的jQuery代码:

$.ajax(
        {
            type: "get",
            dataType: "json",
            url: "check_basket.php",
            data: {bought : bought, pid : pid, cid : cid, csid : csid, lang : lang},
            success: function(data){    
                        if(data.status == 1){
                            alert(data.b);
                        } else if(data.status == 2){
                            alert(data.b);
                        } else if(data.status == 0 ) {
                             $("#dropdown_shopping_cart_inner").html(data);
                             $('#head_shopping_cart').load(document.URL +  ' #head_shopping_cart');
                             $('#dropdown_shopping_cart').slideDown(800);
                        }
            }
        }
    );

此外,在检查状态2时,您检查的是data == 2而不是data.status == 2

将其与简短的get语句结合起来,清理您的代码,您应该可以这样做:

$.get('check_basket.php',function(data){
switch(data.status){
 case 0:
     $("#dropdown_shopping_cart_inner").html(data);
     $('#head_shopping_cart').load(document.URL +  ' #head_shopping_cart');
     $('#dropdown_shopping_cart').slideDown(800);
 break;
 case 1:
    alert(data.b);
    break;
 case 2:
    alert(data.b);
    break;
  }
});

甚至更短:

$.get('check_basket.php',function(data){
  if(data.status == 0){
     $("#dropdown_shopping_cart_inner").html(data);
     $('#head_shopping_cart').load(document.URL +  ' #head_shopping_cart');
     $('#dropdown_shopping_cart').slideDown(800);
  } else{
    alert(data.b);
  }
});

您有两个选择。

  1. 客户端:从中删除dataType: "json"
  2. 服务器端:在此添加json_encode("$output")。此外,您还可以发送一个封装在[]中的空值或null值,因为jQuery将其作为脚本执行

对于服务器端的解决方案,我会说:

echo '["everything ok"]';

完全删除dataType,让jQuery来解决它。

正确构建您的返回消息,以处理成功和失败。

为成功分配从10到20的状态代码,为失败分配从0到9的状态代码

成功的

print json_encode(array(
    'status' => 11,
    'message' => "all is good"
);

对于故障或错误

print json_encode(array(
    'status' => 4,
    'message' => "some things are not so cool"
));

调整您的success回调以处理两种类型的返回

success: function(data){
    // check if you have a success or failure
    if (data.status > 10){
        console.log(data.message);
    }
    else {
        alert(data.message);
    }
}