question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to resolve CORS issue in v3?

See original GitHub issue

Environment

Nuxt CLI v3.0.0-rc.6 00:21:35 RootDir: /Users/user/ryanown/ownwork/abroad-go/web 00:21:38 Nuxt project info: 00:21:38


  • Operating System: Darwin
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-rc.6
  • Package Manager: npm@8.5.0
  • Builder: vite
  • User Config: build, alias, plugins, modules, css, vite
  • Runtime Modules: @nuxtjs/tailwindcss@5.3.1
  • Build Modules: -

Reproduction

Cannot mock a CORS issue here.

Describe the bug

I got a CORS error when I add a bearer token in my request header:

config.headers.Authorization = "Bearer token"

Then I got: Access to XMLHttpRequest at 'https://mysite/api/getUsers' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response. I remember this isn’t gonna happen because we can set a proxy server to handle the CORS issue.

But can can’t make it work by setting the nuxt.config.ts lile:

vite: {
   server: {
        host: "localhost",
        port: 3000,
        proxy: {
          "/": {
            target: "https://mysite/api",
            changeOrigin: true,
          },
        },
        hmr: { overlay: false },
      },
}

The dev server port can even not be changed if I change 3000 to some other numbers like 1997.

haven’t it been supported yet?

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
HuakunShencommented, Aug 19, 2022

@KahrLu The routes setting only works in development mode for me, but doesn’t work in production.

1reaction
qualiatokyoltdcommented, Oct 16, 2022

@HuakunShen

The routes setting only works in development mode for me, but doesn’t work in production.

Too me. may route “cors” options not work. use server middleware to solution.

at server/middleware/cors.ts

export default defineEventHandler((event) => {
  setResponseHeaders(event, {
    "Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
    "Access-Control-Allow-Origin": "*",
    'Access-Control-Allow-Credentials': 'true',
    "Access-Control-Allow-Headers": '*',
    "Access-Control-Expose-Headers": '*'
  })
  if(getMethod(event) === 'OPTIONS'){
    event.res.statusCode = 204
    event.res.statusMessage = "No Content."
    return 'OK'
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Ways to Fix the CORS Error — and How the Access-Control ...
Fix one: install the Allow-Control-Allow-Origin plugin. The quickest fix you can make is to install the moesif CORS extension .
Read more >
CORS errors and how to solve them - Topcoder
Still facing a CORS error? ... Open a network tab in your console. ... In the response header look for the Access-Control-Allow-Origin header....
Read more >
How To Fix CORS Issue Permanently Right Now (2022)
To quickly fix it, use one of the public CORS proxy servers. I use Heroku CORS proxy server in this example. Append the...
Read more >
Fixing Common Problems with CORS and JavaScript
A response can only have at most one Access-Control-Allow-Origin header. The header can only specify only one domain. If the server needs to ......
Read more >
CORS Errors: Cross-Origin Resource Sharing - Ionic Framework
The correct and easiest solution is to enable CORS by returning the right response headers from the web server or backend and responding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found