我有一个用户填写的联系表单,该表单将发布到我的Web API以使用 nodemailer 发送 .

当我在本地运行客户端和API时,CORS按预期工作:

Client

localhost:4200 POST请求

sendEmail(queryObject: any) {
const body = JSON.stringify(queryObject);
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/api/sendemail', body, {headers: headers})
  .map((response: Response) => response.json());
}

API

本地主机:3000

但是一旦我将API部署到heroku并将客户端中的POST URL更改为指向Heroku,我就会收到预检错误:

选项https://keilcarpenter-portfolioapi.herokuapp.com/api/sendemail 503(服务不可用)XMLHttpRequest无法加载https://keilcarpenter-portfolioapi.herokuapp.com/api/sendemail . 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头 . 因此不允许来源'http:// localhost:4200'访问 . 响应具有HTTP状态代码503 .

我确信我在我的API上的server.js中正确设置了CORS:

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

为什么CORS在本地接受POST但不在Heroku上接受?