有沒有辦法從區分開來最新的Web瀏覽器。
W3C規範:
The steps below describe what user agents must do for a simple cross-origin request:
Apply the make a request steps and observe the request rules below while making the request.
If the manual redirect flag is unset and the response has an HTTP status code of 301, 302, 303, 307, or 308 Apply the redirect steps.
If the end user cancels the request Apply the abort steps.
If there is a network error In case of DNS errors, TLS negotiation failure, or other type of network errors, apply the network error steps . Do not request any kind of end user interaction.
Note: This does not include HTTP responses that indicate some type of error, such as HTTP status code 410.
Otherwise Perform a resource sharing check. If it returns fail, apply the network error steps. Otherwise, if it returns pass, terminate this algorithm and set the cross-origin request status to success. Do not actually terminate the request.
正如你可以看到,網絡錯誤不包括HTTP響應,其中包括錯誤的,這就是爲什麼你總是0獲得作爲狀態碼,而「」的錯誤。
Source
注意:使用谷歌的Chrome版本43.0.2357.130作了下面的例子和反對,我已經可以模擬OP一個環境。代碼設置在答案的底部。
我雖然是一種方法來解決,這將是使通過HTTP的二次請求,而不是HTTPS作爲This answer,但我記得這是不可能的,因爲該瀏覽器的新版本阻止混合內容。
這意味着如果您使用HTTPS,Web瀏覽器將不允許通過HTTP請求,反之亦然。
從幾年前就已經是這樣了,但老版本的Web瀏覽器版本比如Mozilla Firefox在它下面版本23允許它。
證據看:
使從HTTP請求HTTPS usign網絡Broser控制檯
var request = new XMLHttpRequest();
request.open('GET', "http://localhost:8001", true);
request.onload = function() {
console.log(request.responseText);
};
request.onerror = function() {
console.log(request.responseText);
};
request.send();
將導致以下錯誤:
Mixed Content: The page at ' https://localhost:8000/ ' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ' http://localhost:8001/ '. This request has been blocked; the content must be served over HTTPS.
同樣的錯誤將出現在瀏覽器控制檯,如果您嘗試以其他方式添加Iframe來完成此操作。
<iframe src="http://localhost:8001"></iframe>
使用Socket連接也Posted as an answer,我敢肯定,其結果將是相同/相似的,但我已經給它一個嘗試。
嘗試打開從Web Broswer使用HTTPS到非安全套接字端點的套接字連接將以混合內容錯誤結束。
new WebSocket("ws://localhost:8001", "protocolOne");
1) Mixed Content: The page at ' https://localhost:8000/ ' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://localhost:8001/'. This request has been blocked; this endpoint must be available over WSS.
2) Uncaught DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
然後我試圖連接到WSS端點也看到如果我可以閱讀有關網絡連接錯誤的一些信息:
var exampleSocket = new WebSocket("wss://localhost:8001", "protocolOne");
exampleSocket.onerror = function(e) {
console.log(e);
}
執行上面的代碼中使用服務器關閉的結果:
WebSocket connection to 'wss://localhost:8001/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
在打開服務器的情況下執行上面的代碼片段
WebSocket connection to 'wss://localhost:8001/' failed: WebSocket opening handshake was canceled
但是,「onerror函數」輸出到控制檯的錯誤沒有任何技巧來區分另一個錯誤。
使用代理作爲this answer suggest可以工作,但只有在「目標」服務器的公共訪問。
這不是這種情況,所以試圖在這種情況下實現代理將導致我們遇到同樣的問題。
代碼來創建Node.js的HTTPS服務器:
我已經創建了兩個HTTPS的NodeJS服務器,使用自簽名證書:
targetServer.js:
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('./certs2/key.pem'),
cert: fs.readFileSync('./certs2/key-cert.pem')
};
https.createServer(options, function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.writeHead(200);
res.end("hello world\n");
}).listen(8001);
ApplicationServer的.js:
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('./certs/key.pem'),
cert: fs.readFileSync('./certs/key-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
要使其正常工作,需要安裝Nodejs,需要爲每臺服務器生成單獨的證書,並相應地將其存儲在文件夾certs和certs2中。
要運行它只需在終端(ubuntu示例)中執行node applicationServer.js
和node targetServer.js
。
我會發布我的代碼,但它不是一個javascript代碼錯誤。請仔細閱讀我的問題。 – taxicala
另外兩個錯誤回調參數是否給出了額外的見解? 'function(xhr,status,msg){...'我懷疑他們會這樣做,但值得嘗試。 –
您是否在不同的瀏覽器中看到不同的錯誤代碼?這可能是由Chrome生成和阻止的。 – Jasen