多个jQuery AJAX调用冲突?

时间:2015-04-01 11:20:45

标签: php jquery ajax

我正在尝试使用AJAX和PHP创建一个简单的购物车。

一切都按原样运作但是有一件事并不能一直工作,而且它似乎无法执行。 (5次中有3次效果)。

解释这个问题请看下面的代码:

jQuery(document).ready(function() {

//////////////////////LETS RUN OUR ADD TO CART FEATURE USING AJAX //////////////    
$(function(){   
    $('.form1').on('submit', function(e){
        $( "#preloader" ).fadeIn( 850 );


        // prevent native form submission here
        e.preventDefault();

        // now do whatever you want here


        $.ajax({
            type: $(this).attr('method'),// <-- get method of form
            url: $(this).attr('action'),
            //url: "cart.php",
            data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
            beforeSend: function(){

            },
            success: function(data){
                $( "#preloader" ).fadeOut( 850 );

            }
        });
    });


    });


//////////////////////LETS RUN LOAD THE cart-header.php ON PAGE LOAD USING AJAX //////////////  
$(document).ready(function () { 
    function load1() {
        $.ajax({ //create an ajax request to load_page.php
            type: "GET",
            url: "cart-header.php",
            dataType: "html", //expect html to be returned                
            success: function (data2) {
                $('#headerCart').html($(data2));



                //setTimeout(load2, 500);


            }

        });

    }

load1();
});

//////////////////////LETS LOAD THE cart-header.php on form1 submit USING AJAX //////////////   

<!----- This is the part that SOMETIMES Fails to work --->


    $(function(){   
    $('.form1').on('submit', function(load2){




        // prevent native form submission here
        load2.preventDefault();

        // now do whatever you want here


        $.ajax({
            type: "GET",// <-- get method of form
            url: "cart-header.php",
            //url: "cart.php",
            dataType: "html", // <-- serialize all fields into a string that is ready to be posted to your PHP file
            beforeSend: function(){

            },
            success: function(data){

                //$('#test').load('cart.php #total').html();

                $('#headerCart').html($(data));





            }
        });
    });


    });


//////////////////////LETS RUN OUR DELETE FROM CART FEATURE USING AJAX //////////////       


$(document).on('submit', '.delForm', function(dleItem){


        // prevent native form submission here
        dleItem.preventDefault();

        // now do whatever you want here


        $.ajax({
            type: $(this).attr('method'),// <-- get method of form
            url: "cart-header.php",
            //url: "cart.php",
            data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
            beforeSend: function(){

            },
            success: function(data){
$('#headerCart').html($(data));
            }
        });
    });




});


//////////////////////LETS GET THE QUANTITY OF CURRENT ITEMS ADDED IN THE CART USING AJAX/////////////


$(document).ready(function () {

    function load() {
        $.ajax({ //create an ajax request to load_page.php
            type: "GET",
            url: "cart.php",
            //url: "cart-header.php",
            dataType: "html", //expect html to be returned                
            success: function (data) {
                $('.item_count').html($(data).find('#total').text());
                //$('#headerCart').html($(data));



                setTimeout(load, 1000);


            }

        });
    }

    load();

});

我已经对代码进行了评论,因此您可以看到代码的各个部分以及它们的作用。

问题在于这一部分:

//////////////////////LETS LOAD THE cart-header.php on form1 submit USING AJAX //////////////   

<!----- This is the part that SOMETIMES Fails to work --->


    $(function(){   
    $('.form1').on('submit', function(load2){




        // prevent native form submission here
        load2.preventDefault();

        // now do whatever you want here


        $.ajax({
            type: "GET",// <-- get method of form
            url: "cart-header.php",
            //url: "cart.php",
            dataType: "html", // <-- serialize all fields into a string that is ready to be posted to your PHP file
            beforeSend: function(){

            },
            success: function(data){

                //$('#test').load('cart.php #total').html();

                $('#headerCart').html($(data));





            }
        });
    });


    });

正如我上面提到的,这段代码工作正常,但只有当它想要有自己的想法时它才有效!

有人可以就此问题提出建议吗?

提前致谢。

0 个答案:

没有答案