有没有办法从区分开来最新的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