CORS is not enabling

Asked

Viewed 895 times

0

I developed a API REST in Node.js and I need to use it in my front-end.

I’ve installed the library Cors and enabled on my server but continues to give the error of access when making the request with AJAX.

Enabling CORS on Node.js

const cors = require('cors');
const app = express();
app.use(cors());

Calling the API in Jquery

$.ajax({
  type:'GET',
  url: `localhost:3030/api/admin/employees`,
  data:fd,
  cache:false,
  contentType: false,
  processData: false,
  success:function(data){
    console.log("success");
    console.log(data);
  },
});

Error in Chrome console

Access to Xmlhttprequest at 'localhost:3030/api/admin/Employees? _=1573153461164' from origin 'http://localhost' has been blocked by CORS policy: Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https

  • 2

    Try swapping the url for http://localhost:3030/api/admin/employees

  • Great, now it’s working =D

1 answer

2


The problem is that you were not putting the full url and it did not identify the server request protocol. Change the url of the ajax request to http://localhost:3030/api/admin/employees should solve your problem as you will use a valid protocol, in the case of HTTP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.