2017-07-11 70 views
0

我的项目有问题。当我提交表单时,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

请帮帮忙,我也没办法了

+0

@ZaheerUlHassan:他提到他的回应状态是成功的,但奇怪的是,他的请求/回复没有任何状态码,像它仍在等待 – Sandy

回答

0

有内置到包含类似form.submit()插件一个submitHandler。既然你是.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; 
      } 
     } 
    }); 

希望它有助于!

相关问题