2011-01-05 58 views
1

我有增加了新的數據通過jQuery的數據庫的頁面上的表單。它工作正常,但有時服務器胡扯了,我得到一個500(內部服務器錯誤),但那是因爲我的服務器太爛。jQuery的:處理內部服務器錯誤響應

的問題是,我有時會收到服務器錯誤的PHP使得除了數據庫後,而是獲得成功的消息,我得到的錯誤,即使除了已經取得進展。那麼我該如何去解決這個問題,以確保PHP在出現錯誤時不會添加數據?

[是的,我最終將轉換到新的服務器,但沒有服務器是完美]

這裏的腳本:

$.ajax({ 
    type: "POST", 
    url: "/clase/do-add", 
    data: $("#add").serialize(), 
    dataType: "json", 
    timeout: 8000, 
    beforeSend: function() { 
    var icon = '<p class="loading-add"></p>'; // loading icon 
    $('form#add').append(icon); 
    }, 
    success: function(data) { 
    if (data.error == true) { // php returns error = true if name is invalid 
     alert('Invalid name.'); 
     $('form#add').find('p').remove(); 
    } else { 
     // make the addition 
    } 
    }, 
    error: function() { 
    $('form#add').find('p').remove(); 
    alert('Error. Try again.'); 
    } 
}); 
+3

是因爲你的服務器太爛或者你的**服務器端代碼太爛了嗎? – 2011-01-05 14:11:30

+0

是你的服務器嗎?你有shell嗎? – jAndy 2011-01-05 14:17:05

+0

這是服務器,當我加載頁面時,我不斷收到各種字符 - ><ÄWÝrÔ¾¾Þ}Å''7»äÏk''¡lh – Norbert 2011-01-05 14:28:02

回答

0

你可以換你Ajax調用的函數和「傳請求「功能再次出錯。

東西臨客這樣的:

jQuery.fn = function yourajaxfunction(){ 
    $.ajax({ 
     type: "POST", 
     url: "/clase/do-add", 
     data: $("#add").serialize(), 
     dataType: "json", 
     timeout: 8000, 
     beforeSend: function() { 
     var icon = '<p class="loading-add"></p>'; // loading icon 
     $('form#add').append(icon); 
     }, 
     success: function(data) { 
     if (data.error == true) { // php returns error = true if name is invalid 
      alert('Invalid name.'); 
      $('form#add').find('p').remove(); 
     } else { 
      // make the addition 
     } 
     }, 
     error: function() { 
     $('form#add').find('p').remove(); 
     // ####### i added this: 
     $(this).yourajaxfunction(); 
     alert('Error. Try again.'); 
     } 
    }); 
} 

或者你可以有你做的錯誤再次運行該功能之前,一些毫秒後再次檢查一個空閒功能:

$(this).idle().yourajaxfunction(); 

空閒功能上面使用:

jQuery.fn.idle = function(time){ 
    var o = $(this); 
    o.queue(function(){ 
     setTimeout(function(){ 
      o.dequeue(); 
     }, time); 
    }); 
    return o; 
}; 

但最好的辦法是修復服務器..這東西我只是黑客和補丁可以彌補另一個問題:服務器不好。 :)

所以它並不適合任何生產場景是一個好主意。

+0

也許你需要檢查最後一個插入=使用jquery + ajax來檢查如果你的表單內容已被保存/插入。但無論如何。這是一堆黑客。我會修復服務器。 :) – 2011-01-05 14:25:28

+0

服務器修復是最好的辦法。在我的htaccess zlib壓縮導致內部服務器錯誤和奇怪的字符。謝謝! – Norbert 2011-01-05 20:31:05

0

您可以創建插件的控制到數據庫中,如果除了返回的錯誤的。

... 
    error: function() { 
     var fError = function() { 

      $('form#add').find('p').remove(); 
      alert('Error. Try again.'); 

      }; 

     $.ajax({ 
      type: "GET", 
      url: "/clase/do-check", 
      data: {id: id}, // id for check 
      dataType: "json", 
      success: function(data) { 
      if (!data.ok) 
       fError(); 
      }, 
      error: function() { 
       fError(); 
      } 
     });    
    } 
    ...