2017-07-11 67 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; 
      } 
     } 
    }); 

希望它有助於!

相關問題