Javascript CORS https对localhost的访问可以在Chrome上使用,但不能在Firefox上使用
我在firefox和IE上遇到了CORS问题,而Chrome似乎可以很好地处理相同的情况 我需要从浏览器中运行的web应用访问本地mongoose服务器。我从我的web应用程序发送XMLHTTP请求,这是一个Google应用程序脚本-HTMLService(由Javascript CORS https对localhost的访问可以在Chrome上使用,但不能在Firefox上使用,javascript,firefox,ssl,google-apps-script,cors,Javascript,Firefox,Ssl,Google Apps Script,Cors,我在firefox和IE上遇到了CORS问题,而Chrome似乎可以很好地处理相同的情况 我需要从浏览器中运行的web应用访问本地mongoose服务器。我从我的web应用程序发送XMLHTTP请求,这是一个Google应用程序脚本-HTMLService(由https://script.google.com)发送到正在本地主机上运行mongoose服务器的本地客户端-127.0.0.1:1234。XMLHTTP请求被发送到 https://localhost:1234/ping 通过HTTP
https://script.google.com
)发送到正在本地主机上运行mongoose服务器的本地客户端-127.0.0.1:1234。XMLHTTP请求被发送到
https://localhost:1234/ping
通过HTTPS(GET)。服务器应以明文形式响应“PING_OK”消息。
我们还将浏览器指向
https://localhost:1234/ping
让我们的测试认证成为可信赖的来源。
完成上述步骤后,Chrome浏览器可以访问本地mongoose服务器,但Firefox失败,并显示消息“NS\u ERROR\u DOM\u BAD\u URI:access to restricted URI denied”(IE也失败)
当从file:///c:test.html
也可以访问
https://localhost:1234/ping
https://localhost:1234
通过Firefox和Chrome
以下是html中的javascript:
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
// set the onreadystatechange callback
xmlhttp.onreadystatechange = function() {
var xStatus = ['UNSENT','OPENED','HEADERS_RECEIVED','LOADING','DONE'];
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText;
console.log('Response: '+response);
} else if((xmlhttp.readyState>0) && (xmlhttp.readyState<4)){
console.log('state='+ xStatus[xmlhttp.readyState]+', status='+ xmlhttp.statusText);
} else {
console.log('state='+ xStatus[xmlhttp.readyState]+', status='+ xmlhttp.statusText);
}
}
// set the timeout value and the ontimeout callback
xmlhttp.timeout = 30000;
xmlhttp.ontimeout = function() {
console.log('XHR timed out');
}
var url = 'https://localhost:1234/ping';
var params = '';
xmlhttp.open('POST', url, true);
xmlhttp.send(params);
当通过google app scripy部署为webapp时,上面的代码在Chrome中运行良好,但Firefox失败,出现“NS_ERROR_DOM_BAD_URI:Access to restricted URI denied”
我对javascript编程比较陌生,我不确定这是我面临的SSL相关问题还是CORS问题
1) 在这种ssl访问中,我是否缺少任何附加参数或配置
2) 还有,为什么从文件://*html提供的页面能够访问
https://localhost:1234/ping
https://localhost:1234
通过CORS,而从
https://script.google.com
在Firefox(和IE)上失败
非常感谢为解决此问题提供的任何指导和建议。我遇到了一个类似的问题,Chrome和IE工作正常,但Firefox在使用Dojo从JavaScript调用WebAPI时失败。事实证明,Firefox发送的“接受”头是不同的,我的API不支持。 Chrome发送以下消息:
Accept: */*
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Firefox发送以下消息:
Accept: */*
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
因此,Firefox收到一个500错误。我通过重写JavaScript调用中的头来解决这个问题。下面是使用Dojo+TypeScript的代码,但是您应该能够在您的环境中执行类似的操作
var d = new Deferred();
var token = this._getToken();
var options = {
method: 'GET',
handleAs: 'json',
headers: {
'Accept': '*/*',
'Authorization': 'Bearer ' + token
}
};
request<Object>(url, options).then(
function (result) {
d.resolve(result);
},
function (err) {
d.reject(err, true);
}
);
return d.promise;
一个更持久的解决方案是让WebAPI处理XML,但我现在不这么做。这似乎是Caja清理的一个公开问题。我们完全被困在这个问题上了。我们正在寻找关于可能的黑客解决方法或替代解决方案的任何建议。@Morris这很有趣。如果我们再次访问该项目并在此处更新,我们将尝试这样做。