CORS issue with Vue.js and Nodejs

2019-07-30 15:31发布

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

1条回答
虎瘦雄心在
2楼-- · 2019-07-30 15:51

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())
}
查看更多
登录 后发表回答