AJAX请求仅显示一个,但控制器调用两次

时间:2017-07-11 10:04:39

标签: javascript jquery ajax laravel laravel-5

我的项目有问题。当我提交表单时,Ajax仅在开发人员工具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

请帮助,我不知道了

1 个答案:

答案 0 :(得分:0)

插件中内置了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;
            }
        }
    });

希望它有帮助!