简体   繁体   English

Node js Heroku 请求的资源上不存在“Access-Control-Allow-Origin”标头

[英]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.

相关问题 Node.JS/Fetch:请求的资源上不存在“Access-Control-Allow-Origin”header - Node.JS/Fetch: No 'Access-Control-Allow-Origin' header is present on the requested resource Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource Access-Control-Allow-Origin header 存在于请求的资源上 - Access-Control-Allow-Origin header is present on the requested resource 请求的资源 .htaccess 上不存在 Access-Control-Allow-Origin 标头 - No Access-Control-Allow-Origin header is present on the requested resource .htaccess XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - Access-Control-Allow-Origin header is not present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - No Access-Control-Allow-Origin header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM