Azer Ben Azzouz
Azer Ben Azzouz

Reputation: 41

When using Axios, in order to pass custom headers The request throws the error

import axios from 'axios';  

var data = JSON.stringify({
    "email": email,
    "password": password
  });
  var config = {
    method: 'post',
    url: 'http://localhost:3001/user/Login',
    headers: {
      'x-api-key': 'apiKey', 
      'content-type':'application/octet-stream',
      'Accept': 'application/json'
    },
    data : data

  axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error);
  });

The Error :

Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:99) Access to XMLHttpRequest at 'http://localhost:3001/user/Login' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

Upvotes: 4

Views: 1722

Answers (1)

Ishan Kesharwani
Ishan Kesharwani

Reputation: 292

Your url has double forward slash but it should be single forward slas

url: 'http://localhost:3001/user/Login',

This might help !!

Upvotes: 1

Related Questions