No 'Access-Control-Allow-Origin' header is present on the requested resource with Vuejs and Nodejs

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



No 'Access-Control-Allow-Origin' header is present on the requested resource with Vuejs and Nodejs



Backend (nodejs)


app.use(function(req, res, next)
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization, Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
next(createError(404));
);



Frontend vuejs


axios.post(`http://localhost:5000/api/v1/users/uploadDocuments?access_token=`+store.state.token,

data: formData,
//accessToken: store.state.token
,
headers:
'Content-Type': 'Application/json',
// 'x-access-token': localStorage.getItem('token')


).then (function (response)
this.potato = response.data
)



I am getting the following error:


Failed to load http://localhost:5000/api/v1/users/uploadDocuments?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.et: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.



I have added Access-Control-Allow-Origin in the backend but the error still persists. How can I fix this ?





Can you see in the network requests that the header is actually being returned to the client? Worth checking.
– Alex Taylor
Aug 13 at 3:39





Allow: POST Connection: keep-alive Content-Length: 4 Content-Type: text/html; charset=utf-8 Date: Mon, 13 Aug 2018 03:28:00 GMT ETag: W/"4-Yf+Bwwqjx254r+pisuO9HfpJ6FQ" set-cookie: connect.sid=s%3AeIZyLhwL8XaaeZoO6jByqsIcrG1B04CM.srODXDxbPwHwn7JwDFy5kq%2FC514bgrstQ5frX%2Bp3qc0; Path=/; Expires=Tue, 13 Aug 2019 03:28:00 GMT; HttpOnly X-Powered-By: Express How do I know whether the header is actually being returned to the client ? @AlexTaylor
– kylas
Aug 13 at 3:45





install chrome cors browser plugin
– Helping hand
Aug 13 at 4:16




2 Answers
2



Try this instead of what you have now


app.use((req, res, next) =>
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin,X-Requested-With,Content-Type,Accept,Authorization"
);
if (req.method === 'OPTIONS')
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET')
return res.status(200).json();

next();
);



Install CORS


npm install cors



Now, include it into app()


var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS