使用 jQuery Ajax 函數時,在指定錯誤和成功處理程序時,您會期望指定的錯誤處理程序函數捕獲無效文件和 404 錯誤,但由于某種原因我的代碼沒有。代碼:// using jQuery 3.4.1// this code has been taken from a larger function, but the issue can be replicated by pasting// the following code into the JS Console.var scriptPath = 'http://mywebserver.localhost/application/invalid.js';jQuery.ajax( { async: false, type: 'GET', url: scriptPath, crossOrigin: false, data: null, global: false, cache: true, success: function() { debugger; // do some stuff onResult(); }, error: function( jqXHR, textStatus, errorThrown ) { debugger; Lib.showNetworkErrorAlert( jqXHR, textStatus, errorThrown ); }, dataType: 'script'} );這導致:未捕獲的錯誤來自:~未知~錯誤:未捕獲的語法錯誤:意外的令牌“<”在: http://mywebserver.localhost/ ........ 行:1澄清一下,我已經查看了那里的 SO 問題,但我發現的問題往往集中在錯誤的原因上。我知道錯誤'Unexpected token < in ...'是什么,它只是JS試圖將目標解析為JS,而是給出了404錯誤html文件,所以第一個標簽中的<觸發了錯誤。我試圖找出和理解的是為什么會出現此錯誤而不是指定的正常錯誤處理程序。至少我會認為將 dataType 設置為 script 會使其實現更好的錯誤,嘿,這是一個 HTML 404 錯誤文件,而不是腳本。非常感謝任何幫助和見解。更新:這是 Dev Tools 中的 Network 選項卡,顯示它正在返回 404。
Ajax Unexpected token < error on 404 而不是普通的 ajax
智慧大石
2022-06-16 16:55:44