Ad

CORS Issue With Vue.js And Nodejs

- 1 answer

I used vuejs with nodejs, with the vue client address http://localhost:8000, the nodejs server addresses http://localhost:3000.

When calling api, I get cors error CORS preflight channel did not succeed request headers

request

Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Host:localhost: 3000 
Origin:http://localhost: 8000

response

Access-Control-Allow-Headers: Origin, X-Requested-With, Accept,content-type
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT,PATH
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Content-Type:application/json; charset=utf-8

error in response is NS_ERROR_DOM_BAD_URI

Ad

Answer

If you use expressjs you can simply use nodejs cors lib to enable CORS in your node server.

I strongly advise you to activate it only for dev purposes :

var cors = require('cors')

if (NODE_ENV !== 'production') {
   app.use(cors())
}
Ad
source: stackoverflow.com
Ad