0

This is my request header

Request URL:http://demo7876167.mockable.io/auth/login
Request Headers
Provisional headers are shown
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Origin:http://localhost:8000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:CACC0804-7333-424B-985E-DBA8164A45E1

this is the exact error which comes on console

OPTIONS http://demo7876167.mockable.io/auth/login net::ERR_EMPTY_RESPONSE

When I try to access the same URL through postman it seems to work. This happens whenever I try to access different server.

Is there any particular way to make calls in angularJs, so that I can post request to any server with CORS enabled

Abhishek Jha
  • 935
  • 2
  • 10
  • 22

1 Answers1

3

The browser is making a preflight OPTIONS request before making the POST request that you are expecting it to make. (Presumably it is the POST request you are testing with Postman).

The preflight request is required if you aren't making a simple request. By default Angular will encode the data in POST requests as JSON (instead of the more common URL Form Encoding). JSON is not one of the acceptable content-types for a simple request.

The server you are making the request to must respond to the OPTIONS request to tell the browser that your JavaScript is allowed to make the JSON Encoded POST request that you are trying to make.

After the browser receives permission, it will make the POST request that you are simulating with Postman.

Alternatively, make a form encoded POST instead of a JSON encoded one.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335