2013-09-25 47 views
5

我正在處理一些現有的代碼。我有以下代碼:

$.ajax({ 
     type: "get", 
     url: url , 
     dataType: "jsonp", 
     cache: "false", 
     jsonpCallback: "onJSONPLoad", 
     success: function(json){ 
       alert('hi!'); 
       //perform operation 
     }, 
     error: function() { 
      alert('Error occurs!'); 
     } 
}); 

現在,當我路過有效url它工作正常。但是當我通過無效url它應該通過錯誤警報。但腳本不會引發任何錯誤警報。基本上我想驗證url參數或它失敗?請幫我找出我的代碼中的錯誤或建議我以其他方式實現。我已經檢查了以下鏈接,但沒能解決我的問題:

jQuery Ajax error handling, show custom exception messages
jQuery ajax error functionAjax Success and Error function failure

UPDATE: 我有下面的代碼添加到登錄jQuery的控制檯日誌。

@Override 
      public boolean onConsoleMessage(ConsoleMessage cm) { 
       Log.d("web chrome client", cm.message() + " -- From line " 
       + cm.lineNumber() + " of " 
       + cm.sourceId()); 
       return true; 
      } 

確定了以下錯誤: XMLHttpRequest cannot load file:///android_asset/xxxx/new_source.json?callback=onJSONPLoad. Origin null is not allowed by Access-Control-Allow-Origin. -- From line 1 of null

加入以下代碼,應用程序運行成功。

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { 
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true); 
} 

所以,基本上這是因爲url不能被jquery訪問。
感謝每一個幫助我的身體。

+0

獲得從控制檯粘貼錯誤.. –

+0

你的答案就在這裏: [首頁] [1] 這裏: [二] [2] [1]:http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages [2]:http://stackoverflow.com/questions/3642348/jquery-ajax-error-callback – AminAmiriDarban

+0

看看我的答案。 –

回答

14

使用該

1)替換此: 數據類型:跨域請求JSONP,這意味着要求到不同的域和

dataType:json爲相同的域相同的來源請求。 數據類型:後 「JSON」

2)你缺少'' 成功功能

$.ajax({ 
     type: "get", 
     url: url , 
     dataType: "json", 
     cache: "false", 
     jsonpCallback: "onJSONPLoad", 
     success: function(json){ 
       alert('hi!'); 
       //perform operation 
     }, 
     error: function() { 
      alert('Error occurs!'); 
     } 
}); 

3)試試這個

error: function(jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       alert('Not connect.\n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       alert('Requested page not found. [404]'); 
      } else if (jqXHR.status == 500) { 
       alert('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       alert('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       alert('Time out error.'); 
      } else if (exception === 'abort') { 
       alert('Ajax request aborted.'); 
      } else { 
       alert('Uncaught Error.\n' + jqXHR.responseText); 
      } 
     } 

4)檢查這個jQuery.ajax

json類型將獲取的數據文件解析爲Java腳本對象並返回構造的對象作爲結果數據。爲此,它在瀏覽器支持時使用jQuery.parseJSON();否則它使用一個Function構造函數。格式錯誤的JSON數據會引發解析錯誤(請參閱json.org獲取更多信息)。 JSON數據便於以簡潔和易於JavaScript解析的方式傳遞結構化數據。如果獲取的數據文件存在於遠程服務器上,請改爲指定jsonp類型。

jsonp類型附加一個查詢字符串參數callback =?到URL。服務器應該用回調名稱預先填充JSON數據以形成有效的JSONP響應。我們可以使用$ .ajax()的jsonp選項指定除回調以外的參數名稱。

注意:JSONP是JSON格式的擴展,需要一些服務器端代碼來檢測和處理查詢字符串參數。有關它的更多信息可以在原始文章中找到,詳細說明它的使用。

當從遠程服務器檢索數據時(只能使用腳本或jsonp數據類型),錯誤回調和全局事件將永遠不會被觸發。

,但你必須會火,那麼代碼:

var req = $.ajax({ 
    url : url, 
    dataType : "jsonp", 
    timeout : 10000 
}); 

req.success(function() { 
    console.log('Yes! Success!'); 
}); 

req.error(function() { 
    console.log('Oh noes!'); 
}); 
+0

謝謝,試試'dataType:「jsonp」'是好的。問題是爲什麼錯誤塊不起作用。 – Yup

+0

如果我使用'dataType:「json」',則錯誤塊有效。但是對於有效的url succes函數不能用作數據返回表單,url是'jsonp'的數據類型。 – Yup

+0

沒有更新不起作用。 – Yup

1

你缺少成功的功能之後逗號,改變數據類型爲JSON

$.ajax({ 
     type: "get", 
     url: url , 
     dataType: "json", 
     cache: "false", 
     jsonpCallback: "onJSONPLoad", 
     success: function(json){ 
       alert('hi!'); 
       //perform operation 
     }, 
     error: function() { 
      alert('Error occurs!'); 
     } 
}); 
+0

謝謝你的回答,逗號在那裏。 – Yup

0

嘗試與此有關。

 $.ajax({ 
       type: "get", 
       url: url , 
       dataType: "jsonp", 
       cache: "false", 
       jsonpCallback: "onJSONPLoad", 
       success: function(result){ 
         alert('hi!'); 
         //perform operation 
       },  
       error: function(req, status, err) { 
       console.log('something went wrong', status, err); 
       alert('something went wrong'+ status + err); 
       } 
     }); 

也讀這個。 http://api.jquery.com/jQuery.getJSON/#entry-examples

,並嘗試 $.getJSON()而不是$.ajax()

+0

我想要提醒錯誤消息。 – Yup

+0

請現在檢查 –

1

最近在Chrome 52(八月2016)引入的錯誤也可能導致這種行爲。跳躍,它不會持續很長時間。

https://bugs.chromium.org/p/chromium/issues/detail?id=633696

,因爲它觸發僅適用於GET請求從onSuccess處理開始它不嚴格相關的初始問題,但我說出來,別人誰可能在這個問題上搜索幫助。