Reputation: 4618
I'm writing an angular application, and i'm trying to connect to our API using the $http
service.
$http.post('https://backend-test/***/v001/login', {'userName': 'admin', 'password': 'passtest'}, {headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}}).then(function success(response) {
console.log(response);
});
However, i keep on getting this error:
XMLHttpRequest cannot load https://backend-test/***/v001/login. Response for preflight has invalid HTTP status code 403
Also it seems to send an OPTIONS request instead of a POST request:
Request Method:OPTIONS
The strange thing is that it is working correctly when i use this tool:
Any thoughts?
Upvotes: 4
Views: 19635
Reputation: 3534
Here, Cors middleware for exressJS (NodeJS)
npm install cors
Code :
var cors = require('cors'),express = require('express');
var app = express(); // defining app using express
app.use(cors()); // **core as middleware**
Upvotes: 1
Reputation: 51
I had the same problem. My backend for this is a tomcat server. I solved it by configuring web.xml like this:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>
</init-param>
</filter>
Upvotes: 5