Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular POST cross origin error while POSTMAN works

I try to POST from my angular login service:

$http.post('https://xyz/login',
            {
                headers: {
                    'Content-type': 'application/json',
                    'Accept': 'application/json',
                    'signature': 'asd'
                }

And I get this error:

XMLHttpRequest cannot load https://xyz/login. 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:1337' is therefore not allowed access.

I tried this headers:

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

And also these:

"Access-Control-Allow-Origin": "*";
"Access-Control-Allow-Headers": "X-Requested-With";
"Access-Control-Allow-Methods": "GET, POST", "PUT", "DELETE";

The interesting thing, is that the POSTMAN works. What shoud I have to do?

Thanks.

like image 668
user3712353 Avatar asked Dec 23 '15 15:12

user3712353


People also ask

How do you resolve CORS error in Postman?

How to unblock yourself. Install the Postman Desktop Agent for your OS on our download page. Note: The CORS error generally happens due to browser limitations regarding resources shared between different internet domains.

Does CORS work with Postman?

Postman simply doesn't care about CORS headers. So CORS is just a browser concept and not a strong security mechanism. It allows you to restrict which other web apps may use your backend resources but that's all.

What is CORS error in angular?

CORS issue occurs in web application if your backend server (your service) is running on a different domain and it is not configured properly. Note: Even if your backend server is running on a localhost with a different port it is treated as a different domain.


1 Answers

Your request includes non-simple headers Content-type and signature which must be included in the response's Access-Control-Allow-Headers header.

(Content-type is sometimes a simple header, but only for particular values. application/json is not one of those values, and it causes Content-type to become non-simple.)

Add Content-type to Access-Control-Allow-Headers in your server's preflight response.

POSTMAN is not bound by the same-origin policy, so it does not require CORS support from the server.

like image 134
apsillers Avatar answered Sep 22 '22 17:09

apsillers