无法在一个脚本中进行多个ajax调用

时间:2016-04-12 06:47:28

标签: javascript jquery ajax jsp

我有一个ajax调用,根据下拉列表自动填充所有字段。然后我需要在单击更新按钮时调用第二个ajax函数。但是当我保持多个ajax调用时,没有一个调用正在工作。请帮忙。

    <script>
    $(document).ready(function(){
        var selected;
        //alert('jquery works');
        $('#selectUsers').change(function(){
            selected = $('#selectUsers').val();
            //alert($('#selectUsers').val());
              $.ajax({
                url: "CustServlet2",
                type: "Post",
                data: {"selectUsers":selected},
                success : function(data)
                    {
                        //alert("Returned data"+ data);
                        var myarr = data.split(" ");
                        $('#cid').val(myarr[0]);
                        $('#csn').val(myarr[2]);
                        $('#req').val(myarr[3]);
                        $('#loc').val(myarr[4]);
                        $('#dtid').val(myarr[5]);
                        $('#dt').val(myarr[6]);
                        $('#dltid').val(myarr[7]);
                        $('#dltn').val(myarr[8]);
                        $('#dpo').val(myarr[9]);
                        $('#dao').val(myarr[10]);
                        $('#rbid').val(myarr[11]);
                        $('#rbn').val(myarr[12]);
                        $('#prid').val(myarr[13]);
                        $('#prn').val(myarr[14]);
                        $('#sid').val(myarr[15]);
                        $('#sn').val(myarr[16]);
                        $('#comments').val(myarr[17]);
                        $('#cci').val(myarr[18]);
                        $('#aci').val(myarr[19]);


                    }
                });
        });
         $("#submit").click(function(){
            //e.preventDefault();
            alert("submit alert");
            var cn1= $('#selectUsers').val();
            var csn1= $('#csn').val();
            var req1= $('#req').val();
            var loc1= $('#loc').val();
            var dltid1= $('#dltid').val();
            var dtid1= $('#dtid').val();
            var rbid1= $('#rbid').val();
            var prid1= $('#prid').val();
            var cci1= $('#cci').val();
            var aci1= $('#aci').val();
            var dpo1= $('#dpo').val();
            var dao1= $('#dao').val();
            var sid1= $('#sid').val();

            alert();
               $.ajax({             

                 url: "CustServlet3",
                type: "Post",
                data: {"selectUsers":cn1 "csn":csn1 "req":req1 "loc":loc1 "dltid":dltid1 "dtid":dtid1 "rbid":rbid1 "prid":prid1 "cci":cci1 "aci":aci1 "dpo":dpo1 "dao":dao1 "sid":sid1},
                success : function(data)
                    {
                    if(data.equals(1)){alert("profile updated successfully !!")}
                    if(data.equals(0)){alert("profile not updated !!")}
                    }//for success 

                });//for ajax2  
        });//for click   
    });


 </script>

2 个答案:

答案 0 :(得分:0)

阻止默认点击,以便您的ajax可以完成&amp;从ajax身体中删除警报

 $("#submit").click(function(e){
            e.preventDefault();
            statname = $('#sn').val();
            alert("statname" +statname);
               $.ajax({             
                 url: "CustServlet3",
                type: "Post",
                data: {sn:statname},
                success : function(data)
                    {
                    if(data.equals(1)){alert("profile updated successfully !!")}
                    if(data.equals(0)){alert("profile not updated !!")}
                    }//for success 

                });//for ajax2  
        });//for click  
    });

答案 1 :(得分:0)

您需要从ajax选项中删除警报

$("#submit").click(function(){
        alert("submit alert"); 
        statname = $('#sn').val();
        alert("statname" +statname);
           $.ajax({             
            alert("ajax working alert"); // Remove this alert from here you cant add alert in between of ajax Options as its Json Object 
             url: "CustServlet3",
            type: "Post",
            data: {"sn":statname},
            success : function(data)
                {
                if(data.equals(1)){alert("profile updated successfully !!")}
                if(data.equals(0)){alert("profile not updated !!")}
                }//for success 

            });//for ajax2  
    });