同一页面中的两个Ajax无法正常工作

时间:2015-04-30 08:39:59

标签: jquery ajax

我在同一页面上有2个ajax调用。第一个工作正常,但第二个没有,

这是我的代码

$(document).ready(function() {
    $(".setDefault").click(function(){         
        var shipId=$(".setDefault:checked").val();
         $.ajax({
             type: "POST",
             url: "ajx-scripts.php",
             data: {shippingId:shipId},
             success: function(data) {
            }
        });
    });
    $(".remove-address").click(function(){
        var id=$(this).attr("value");
        alert(id);
         $.ajax({
             type: "POST",
             url: "ajx-scripts.php",
             data: {shipId:id},
             success: function(data) {
                 alert(data);
            }
        });
    });
});

id的警报正在运行。 shipId和shippingId是不同的

ajx-scripts.php如下

if(isset($_POST["shippingId"])){
    $shippingId=$_POST["shippingId"];
    $rslt=mysql_query("UPDATE tbl_shipping_addresses AS s, (SELECT fkCustomer FROM tbl_shipping_addresses WHERE shippingId='$shippingId') AS p SET s.setDefault='0' WHERE s.fkCustomer=p.fkCustomer");
    mysql_query("update tbl_shipping_addresses set setDefault=1 where shippingId='$shippingId'");
 }

if(isset($_POST["shipId"])){
    $shippingId=$_POST["shipId"];
    $rslt=mysql_query("select fkCustomer from tbl_shipping_addresses where shippingId='$shippingId'");
    $row=mysql_fetch_assoc($rslt);
    $customer=$row['fkCustomer'];
    mysql_query("delete from tbl_shipping_addresses where shippingId='$shippingId'");
    echo "deleted";
}

1 个答案:

答案 0 :(得分:1)

两者都有不同的参数。如果你正在检查两个索引的发送数据参数,那么它应该工作,否则应该是问题。

在第二次ajax调用中试试这个:

data: {shippingId:id},

而不是

data: {shipId:id},

希望这样会有问题。

您还可以通过添加.error函数来检查ajax调用中的错误:

$(".remove-address").click(function(){
    var id=$(this).attr("value");
    alert(id);
     $.ajax({
         type: "POST",
         url: "ajx-scripts.php",
         data: {shippingId:id},
         success: function(data) {
             alert(data);
        },
        error: function(x,a,y){ //add this error function
            alert(JSON.stringify(x)+" "+a);
        }
    });
});