Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多个jQuery AJAX调用冲突?_Php_Jquery_Ajax - Fatal编程技术网

Php 多个jQuery AJAX调用冲突?

Php 多个jQuery AJAX调用冲突?,php,jquery,ajax,Php,Jquery,Ajax,我正在尝试使用AJAX和PHP创建一个简单的购物车 一切都正常工作,但有一件事不是一直都正常工作,似乎无法执行(5次中有3次有效)。 要解释此问题,请查看下面的代码: jQuery(document).ready(function() { //////////////////////LETS RUN OUR ADD TO CART FEATURE USING AJAX ////////////// $(function(){ $('.form1').on('submit'

我正在尝试使用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();

});
jQuery(文档).ready(函数(){
//////////////////////让我们使用AJAX运行“添加到购物车”功能
$(函数(){
$('.form1')。关于('submit',函数(e){
$(“#预加载程序”).fadeIn(850);
//禁止在此处提交本机表单
e、 预防默认值();
//现在你想怎么做就怎么做
$.ajax({
类型:$(this.attr('method'),//
$(函数(){
$('.form1')。在('submit',函数(load2)上{
//禁止在此处提交本机表单
load2.preventDefault();
//现在你想怎么做就怎么做
$.ajax({
键入:“获取”、//
$(函数(){
$('.form1')。在('submit',函数(load2)上{
//禁止在此处提交本机表单
load2.preventDefault();
//现在你想怎么做就怎么做
$.ajax({

类型:“GET”、//
数据类型:“html”
是因为ajax应该是
html
格式的结果
在success函数中,它位于另一个doc ready的作用域中。将其置于全局作用域中。@Jai,我只是将其作为测试放在那里。我可以删除它,但仍会执行相同的操作(在需要时工作)。
//////////////////////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));





            }
        });
    });


    });