【问题标题】:AJAX Request Show Only One, but Controller called twiceAJAX 请求只显示一个,但控制器调用了两次
【发布时间】:2017-12-15 07:58:31
【问题描述】:

我的项目有问题。当我提交表单时,Ajax 仅在 Developers Tool Mozilla 中请求 1,但突然在服务器中好像请求调用了两次(我在代码中添加日志进行检查)。 有时这个问题不会发生,但会发生更多。 是否可能与稳定的互联网连接有关?

这是我的 ajax。

$('#GIForm').submit(function(e) {
        e.preventDefault();
    }).validate({
        submitHandler:function(form){
            if(countSave == 0){
                $.ajax({
                    url:'/new',
                    method:'post',
                    async: false,
                    data: {
                        date : $('#GIDate').val(),
                        notes : $('#GINotes').val(),
                        mswarehouse_id : $('#GISourceWarehouse').val(),
                        mstransactiontype_id : $('#GIType').val(),
                        dest_msoutlet_id : $('#GIOutlet').val(),
                        msvendor_id : $('#GIVendor').val(),
                        detail : detail
                    },
                    success: function(response){
                        $('#pleaseWaitDialog').modal('hide');
                        if(response.status=='success'){
                            var message = 'Document ' + response.description.document + ' has been saved!';
                            showAlert('',message,'success',function(e){
                                window.location.replace(laroute.route('admin.goods_issue.index'));
                            });
                        }else{
                            showAlert('',response.description,'error');
                            $('#addGIbtn').prop('disabled',false);
                            countSave = 0;
                        }
                    }, error:function(xhr,text, status){
                        $('#pleaseWaitDialog').modal('hide');
                        $('#addGIbtn').prop('disabled',false);
                        countSave = 0;
                        if(xhr.status==422){
                            showAlert('',xhr.responseJSON.join('\n'),'error');
                        }
                    }
                });
                return false;
            }
        }
    });

这是来自 Dev 的图片。工具 Mozilla。我模糊了域。 我不知道为什么颜色状态响应只有灰色,并且响应已经返回状态成功。 这个问题使我的数据库插入两次。 这是我的Mozilla的截图。 Response from Network Dev Tools Mozilla

请帮忙,我不知道了

【问题讨论】:

  • @ZaheerUlHassan :他提到他的响应状态是成功但奇怪的是,他的请求/响应没有任何状态代码,就像它仍在等待中一样

标签: javascript jquery ajax laravel laravel-5


【解决方案1】:

插件中内置了一个submitHandler,其中包含form.submit() 之类的内容。由于您是 .submit() 的表单,因此不需要。所以删除$('#GIForm').submit(function(){})

 $('#GIForm').validate({
        submitHandler:function(form){
            if(countSave == 0){
                $.ajax({
                    url:'/new',
                    method:'post',
                    async: false,
                    data: {
                        date : $('#GIDate').val(),
                        notes : $('#GINotes').val(),
                        mswarehouse_id : $('#GISourceWarehouse').val(),
                        mstransactiontype_id : $('#GIType').val(),
                        dest_msoutlet_id : $('#GIOutlet').val(),
                        msvendor_id : $('#GIVendor').val(),
                        detail : detail
                    },
                    success: function(response){
                        $('#pleaseWaitDialog').modal('hide');
                        if(response.status=='success'){
                            var message = 'Document ' + response.description.document + ' has been saved!';
                            showAlert('',message,'success',function(e){
                                window.location.replace(laroute.route('admin.goods_issue.index'));
                            });
                        }else{
                            showAlert('',response.description,'error');
                            $('#addGIbtn').prop('disabled',false);
                            countSave = 0;
                        }
                    }, error:function(xhr,text, status){
                        $('#pleaseWaitDialog').modal('hide');
                        $('#addGIbtn').prop('disabled',false);
                        countSave = 0;
                        if(xhr.status==422){
                            showAlert('',xhr.responseJSON.join('\n'),'error');
                        }
                    }
                });
                return false;
            }
        }
    });

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多