[英]Node js Heroku No 'Access-Control-Allow-Origin' header is present on the requested resource
I have the following headers setup in my node js api app:我在我的节点 js api 应用程序中设置了以下标头:
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested, Content-Type, Accept Authorization"
);
if (req.method === "OPTIONS") {
res.header(
"Access-Control-Allow-Methods",
"POST, PUT, PATCH, GET, DELETE"
);
return res.status(200).json({});
}
next();
});
With this config I can send GET, POST request to my api hosted in heroku from Postman.通过这个配置,我可以从 Postman 向我在 heroku 中托管的 api 发送GET、POST请求。 But when I try from my frontend app built with vue.
但是当我尝试使用 vue 构建的前端应用程序时。 I get the following error.
我收到以下错误。
And I'm using fetch to send the request to remote api:我正在使用 fetch 将请求发送到远程 api:
async signup() {
try {
const formData = new FormData();
formData.firstName = this.firstName;
formData.lastName = this.lastName;
formData.email = this.email;
formData.password = this.password;
formData.confirmPassword = this.password;
formData.mobile = this.mobile;
formData.gender = this.gender;
formData.profileImg = this.profileImg;
const response = await fetch(
"https://api-url.com/auth/patient/signup",
{
body: formData,
method: "POST",
}
);
const data = await response.json();
this.response = JSON.stringify(data);
} catch (error) {
console.log(error);
}
}
Can anyone point the mistakes I've made here ?谁能指出我在这里犯的错误?
I think you need to set the headers in the fetch call.我认为您需要在 fetch 调用中设置标头。 I see that you have already added body and method.
我看到您已经添加了正文和方法。
headers: { 'Content-Type': 'application/json' }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.