我试图建立一个GET请求跨域。域A是目标,它有一个名为/服务的getUser:无法获取CORS的工作(NodeJS)
(...)
server.get('/getUser', function(req, res) {
console.log('Call received');
}
(...)
正如你可以看到它并没有做太多,我只是想以确保请求到达。 域A有一个公共js文件,它实现了调用该服务:
function callDomainA(){
var url = 'https://domainA.com/getUser?callback=?';
var xhr = _createCORSRequest('GET', url);
if (!xhr) {
console.log('CORS not supported by browser, try jsonp');
_doJSONP(url)
}
else{
xhr.onload = function() {
console.log('Success : ' + JSON.stringify(xhr.responseText));
};
xhr.onerror = function() {
console.log('ERROR : There was an error with the CORS request.');
};
}
xhr.send();
return false;
}
function _doJSONP(){
(...)
}
function _createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr)
xhr.open(method, url, true); // XHR for Chrome/Firefox/Opera/Safari.
else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest(); // XDomainRequest for IE.
xhr.open(method, url);
}
else
xhr = null; // CORS not supported.
return xhr;
}
然后,我有域B,它加载从域A的js脚本,然后执行callDomainA()函数:
<script src="https://domainA.com/domainApublicjsfile.js" type="text/javascript"></script>
(...)
callDomainA();
(...)
当我加载包含对域A的调用的域B中的页面并触发对callDomainA()的调用时,代码检测到浏览器(Firefox)可以执行CORS,但收到的标头指示错误500和ns_error_dom_bad_uri
我期望这样的事情,因为我没有在域A中接受CORS请求或任何东西,但它担心我的请求似乎没有到达服务,或者至少是console.log()没有注册任何东西。
我错过了什么?
预先感谢您的时间。
简化这一点。使用curl或wget向'/ getUser'发出请求。它工作吗?可能不会,如果不是,则删除所有客户端问题。 – Brad