我是node.js的新手并且遇到了cors问题.我正在从一个快递服务器向另一个快递服务器发送客户端ajax get请求,但至少我无法从app.all('*', function(req, res, next){console.log("request: "+req)})
GET请求获得响应,如下所示:
$.ajax({
type: 'GET',
url: 'http://localhost:8082/request'
});
Run Code Online (Sandbox Code Playgroud)
我还在ajax中设置标题:
'beforeSend : function(xhr) {
xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Max-Age', '1000');
xhr.setRequestHeader('Authorization', '*')},
Run Code Online (Sandbox Code Playgroud)
...或在node.js中设置cors模块:
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*',cors(),function(){
console.log("preflight")
});
Run Code Online (Sandbox Code Playgroud)
在firefox和chrome中,我没有得到任何响应(我得到了chrome net::ERR_CONNECTION_REFUSED)当我在PC上本地运行节点服务器时,一切正常.curl命令工作正常.有机会,这是托管/端口的问题还是我还缺少什么?