2012-04-24 55 views
2

我使用jQuery autocomple與遠程數據源事件錯誤,但是,有時,當嘗試在我的服務器做搜索將返回一個錯誤,因爲我使用的是其他Web服務類似的數據源。 我想知道我的web服務返回的狀態代碼,並打印錯誤信息 例:獲取jQuery的自動完成遠程數據源

$(idObjeto).autocomplete({ 
      source:url, 
      minLength: 3, 
      select:function(data,ui){ 
       $(formatIdJQuery(idObjValueReceptor)).val(ui.item.id); 
      } 
     }).data("autocomplete")._renderItem = function(ul, item) { 
     return $("<li></li>") 
      .data("item.autocomplete", item) 
      .append("<a>" + item.label + " - <strong>" + item.id + "</strong></a>") 
      .appendTo(ul); 
     }; 
    } 

假設什麼我的web服務返回的狀態碼404,我想獲得這個狀態代碼和呼叫警報窗口,例如。

這是所有鄉親!

回答

4

你可以重新構造您的Widget使用函數作爲source參數,然後進行Ajax請求自己,做任何你想在一個錯誤:

$(idObjeto).autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: url, 
      dataType: "json", 
      data: request, 
      success: function (data) { 
       response(data); 
      }, 
      error: function() { 
       response([]); // send no results to the widget. 
       alert("an error occurred!"); 
      } 
     }); 
    }, 
    minLength: 3, 
    select:function(data,ui){ 
     $(formatIdJQuery(idObjValueReceptor)).val(ui.item.id); 
    } 
}).data("autocomplete")._renderItem = function(ul, item) { 
    return $("<li></li>") 
     .data("item.autocomplete", item) 
     .append("<a>" + item.label + " - <strong>" + item.id + "</strong></a>") 
     .appendTo(ul); 
    }; 
}; 
+0

謝謝,這很好地工作 – 2012-04-24 14:14:43

相關問題