简体   繁体   English

节点后端Vue(Vite)前端,Axios请求CORS错误

[英]Node Backend Vue (Vite) Frontend, Axios request CORS Error

I know this issue had been discussed a lot on Stackoverflow but unfortunately I just couldn't find a way to make it work.我知道这个问题在 Stackoverflow 上已经讨论了很多,但不幸的是我找不到让它工作的方法。 Long story short, I am building an API using Node JS to that makes a request to MailChimp, and I am calling this API on the client side Vue JS app with Vite.长话短说,我正在使用 Node JS 构建一个 API 来向 MailChimp 发出请求,我在客户端 Vue JS 应用程序上使用 Vite 调用这个 API。 The API works fine with Postman, but it returns Access to XMLHttpRequest at from origin has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource error. The API works fine with Postman, but it returns Access to XMLHttpRequest at from origin has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource错误。

Here is the index js file on the server:这是服务器上的索引js文件:

const cors = require('cors')
app.use(cors({ origin: '*' }))
app.use('/v1/crm/leads', require('./src/routes/LeadRoutes'))

Here is my controller method这是我的 controller 方法

await addSubscriberToAudience('xxx', req.body.email).then(async (mailchimpID) => {
  if (mailchimpID) {
    let data = {
      references: {
        mailchimpID: mailchimpID
      }
    }
    let updatedLead = await Lead.findByIdAndUpdate(lead.id, data, { new: true })
    res.status(200).json({ data: updatedLead, message: 'New Lead Created Successfully' })
  } else {
    console.log('Null Mailchimp ID')
  }
})

Here is how I call it from the frontend:这是我从前端调用它的方式:

axios.post(APIURL + '/v1/crm/leads/create', data, {
        headers: { 'X-ORIGIN': 'https://example.com' }
      }).then((response) => {
        console.log(response)
      }).catch((error) => {
        console.log(error.message)
      })

I can see Access Allow Origin is set in the response header in the preflight response:我可以看到在预检响应中的响应 header 中设置了访问允许来源: 在此处输入图像描述

Backend is hosted on AWS Elastic Beanstalk and frontend is hosted on AWS S3 Bucket Any help is needed please, and thank you!后端托管在 AWS Elastic Beanstalk 上,前端托管在 AWS S3 Bucket 上 请需要任何帮助,谢谢!

Edit 1: I am making other POST API calls from the website to my backend server, they all work fine.编辑 1:我正在从网站向我的后端服务器进行其他 POST API 调用,它们都工作正常。 Only this one is not working, and the difference is that, after I make the call from client side to the server, server calls Mailchimp API, and then waiting on response from Mailchimp API before return a success response to my frontend, hope this context helps只有这个不起作用,不同的是,在我从客户端调用服务器后,服务器调用 Mailchimp API,然后等待 Mailchimp API 的响应,然后将成功响应返回给我的前端,希望这个上下文帮助

Wich browser are you using?你用的是哪个浏览器? Apparently on Chrome the wildcard * does't match the localhost url.显然在 Chrome 上,通配符 * 与本地主机 url 不匹配。 There is a ppost here on stackoverflow about this: stackoverflow上有一个关于这个的ppost:

Why does my http://localhost CORS origin not work? 为什么我的 http://localhost CORS 原点不起作用?

And here is the documentation about chrome:这是关于 chrome 的文档:

Chromium Doc 铬文档

If you are perfectly sure this CORS errors only occurs on a single POST endpoint (other POSTs work);如果您完全确定此 CORS 错误仅发生在单个 POST 端点上(其他 POST 工作); then this is usually caused by a simple error in the endpoint Controller.那么这通常是由端点 Controller 中的一个简单错误引起的。

What I think happens is app.use(responseMiddleware) adds the responseMiddleware actually to the successful responses, but not for failure responses (eg. 500 s).我认为发生的是app.use(responseMiddleware)实际上将responseMiddleware添加到了成功的响应中,但不适用于失败的响应(例如500秒)。

I believe Chrome is lying, and CORS header was actually found in the OPTION response, but not in the actual POST response.我相信 Chrome 在撒谎,并且 CORS header 实际上是在OPTION响应中找到的,但在实际的POST响应中却没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM