1

我使用了两个 nodejs(无 Express)应用程序:

webprocess 数据处理

dataprocess 使用以下代码发回一条休息消息:

var status = 200;
if (responseStatus) {
    status = responseStatus;
}
var contentType = "application/json; charset=utf-8";
if (responsecontentType) {
    contentType = responsecontentType;
}
this.response.statusCode = status;
//http://www.w3.org/TR/cors/
//The server allows any domain to call it with the XMLHttpRequest
this.response.setHeader("Access-Control-Allow-Origin", this.request.headers.origin);
//The server allow the content-type header
this.response.setHeader("Access-Control-Allow-Headers", "Content-Type");

this.response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
this.response.setHeader("Content-Type", contentType);
this.response.end(contentText);

在本地,一切正常,“Access-Control-Allow-Origin”工作正常。但是,当我在 heroku 上创建两个应用程序并部署时,我在飞行前 ajax 调用 (OPTIONS) 上得到了浏览器跨域异常。不知道出了什么问题?

谢谢你的帮助

约安

4

1 回答 1

2

If you want to allow any domain to call your endpoint as per the comment in your code, you would use a wildcard instead of this.request.headers.origin

//The server allows any domain to call it with the XMLHttpRequest
this.response.setHeader("Access-Control-Allow-Origin", "*");
于 2013-05-14T04:40:40.020 回答