問題描述
我正在編寫一個向遠程服務器發出 HTTP 請求的 javascript 應用程序.用戶將輸入主機名.
I am writing a javascript app that makes an HTTP request of a remote server. The user will enter the host name.
如果他們輸入的 DNS 名稱無法解析,我想提供診斷消息.這是當前代碼:
I want to offer a diagnostic message if they enter a DNS name that cannot resolve. Here's the current code:
var req, t, url;
url = 'http://definitelydoesntexist0x314159.com';
req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
t = req.statusText;
}
};
req.send();
在 onreadystatechange 函數中,req 的 status=0,response 是 "",所以沒有太多跡象表明出了什么問題.然而,控制臺顯示加載資源失敗:net::ERR_NAME_NOT_RESOLVED",因此瀏覽器 (Chrome) 能夠弄清楚發生了什么.
In the onreadystatechange function, the req has status=0, response is "", so there's not much indication of what went wrong. Yet the Console shows "Failed to load resource: net::ERR_NAME_NOT_RESOLVED" so the browser (Chrome) was able to figure out what happened.
如何獲得 ERR_NAME_NOT_RESOLVED 的指示?
How can I get an indication of ERR_NAME_NOT_RESOLVED?
更新:我回到了這個問題,使用的策略是任何不是超時"的響應都意味著名稱已解析、主機回答等.
Update: I have come back to this question, using the strategy that any response that isn't 'timeout' means that the name resolved, the host answers, etc.
我最初問題的答案似乎是:瀏覽器本身(在本例中為 Chrome)檢測到無法解析并將其顯示在控制臺中,但 XMLHttpRequest API 不夠豐富,無法指示原因.因此,可憐的 Javascript 程序員只能將超時作為一種解決方法.
The answer to my original question seems to be: It appears that the browser itself (Chrome, in this case) detects the failure to resolve and displays it in the Console, but the XMLHttpRequest API isn't rich enough to indicate the cause. So the poor Javascript programmer is stuck with the timeout as a workaround.
我還刪除了 CORS 標頭,因為其中一位正確指出的評論者沒有任何價值.
I also removed the CORS header, as one of the commenters correctly noted was of no value.
推薦答案
經過大量研究,我更清楚地理解了問題,并且理解了答案(不,不可能得到原因 - 例如,ERR_NAME_NOT_RESOLVED -處于錯誤狀態).
After a great deal more research, I understand the problem more clearly, and understand the answer (No, it's not possible to get the reason - e.g., ERR_NAME_NOT_RESOLVED - in the error status).
這是設計使然. Javascript 錯誤處理例程不會也不能返回有關失敗原因的更多信息.瀏覽器這樣做是為了防止惡意 Javascript 代碼在本地網絡上運行端口掃描器并將該信息發送到遠程服務器.
This is by design. The Javascript error handling routines do not, and must not, return more information about the reason for the failures. Browsers do this to prevent malicious Javascript code from running port scanners on the local network and sending that information to a remote server.
我對 Chromium 錯誤報告者的報告和其中一位開發人員的回復非常清楚地證明了這一點,網址為:https://bugs.chromium.org/p/chromium/issues/detail?id=718447 特別是評論 #2.
This is borne out quite clearly by my report on the Chromium bug reporter and the response from one of the developers, at: https://bugs.chromium.org/p/chromium/issues/detail?id=718447 especially Comment #2.
因此,窗口中的 javascript 在沙箱中運行,該沙箱只能訪問加載、超時、中止和錯誤狀態,沒有任何進一步的粒度.(不過,瀏覽器的調試環境可以顯示此信息,因此您可以找出問題所在...)
Thus, the javascript in the window runs in a sandbox, that only has access to load, timeout, abort, and error status, without any further granularity. (The browser's debugging environment, though, can display this information, so you can figure out what went wrong...)
這篇關于XMLHttpRequest() &凈::ERR_NAME_NOT_RESOLVED的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!