1

I am trying to do a an HTTP POST to a server.

The data I have to send is a json object.

The problem is that $http.post in angular override the method with options.

I can make this config

.config(['$httpProvider', function ($httpProvider) {
  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}])

and changes from options to POST, but I can't set the content-type to "application/json", and I am getting a "415 Unsupported Media Type"

Thank you

1
  • Do you have control of the server you are accessing? It might not support an OPTIONS method request. You can't avoid preflight when using CORS via $httpProvider. Commented Mar 19, 2014 at 1:23

2 Answers 2

6

$http.post in angular doesn't override the method with OPTIONS. It appear that you are trying to call api in different domain than the one your JS code come from. This is called Cross Domain. For such cases the browser performs preflight request with OPTIONS in order to see the returned headers. In your backend response you should add the header Access-Control-Allow-Origin: * for example. When the browser sees that header he performs the actual POST request.

More details here: https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

Hope this is helps!

Sign up to request clarification or add additional context in comments.

6 Comments

thank you, that's exactly what it is happening, but I already have the Access-Control-Allow-Origin: * in the server, and I get the same error.
@agusgambina: Open devtools in chrome, which exception exactly do you get in console?
The server doesn't allow CORS, so I am looking for which options I have available, one is to implement a proxy to make the requests. Thanks for the answers @Naor.
@Naor how do you provide a backend response that allows the cross-domain ?
@Naor I am POSTing to a POST route set up on Sinatra running on heroku
|
1

Add

$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';

But note this will set the Content-Type header globally.

If you need to set the content-type per call, you should use $http.post like

 $http.post("/foo/bar", requestData, {
        headers: { 'Content-Type': 'application/json'},
        transformRequest: transform
    }).success(function(responseData) {
        //do stuff with response
    });

3 Comments

thank you for the reply. I tried both, put it globally and local in the function, but the same result, change the request method to options.
Shouldn't have to do this, the default for $http.post is application/json. See /docs/api/ng/service/$http for your version, search for 'Setting HTTP Headers'.
this actually worked for me. had to use $http.post(url, data) instead of $http({ method: 'POST', data: data })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.