14

I would like to specify dataType: 'json' as in conventional jQuery $.ajax. Is this possible with Angular.js $http.post ?

3 Answers 3

20

You can use the HTTP Config object to set the headers:

$http({
    method: 'POST',
    url: 'somewhere.xyz',
    headers: {
        'Content-type': 'application/json'
    }
})
Sign up to request clarification or add additional context in comments.

3 Comments

good, i only extend comment before. You'll need to setup headers always if you are sending json objects as response from server side. Normal classic print({id: 11}) may throw you error. Setup header content-type as json and should works fine. Cya
ARGGGGH dude, u just saved my day! I've been pulling my hair out wondering why the hell my Node/Express4 wouldn't parse POST data... turns out I had to forse the header content type in my angular $http requests..... god dammit!
There is no need to set the above because it is the default. See: docs.angularjs.org/api/ng/service/$http#setting-http-headers. The fact that this helped you, probably indicates that your code was setting the default header someplace else to another value.
5

From http://docs.angularjs.org/api/ng.$http

Transforming Requests and Responses Both requests and responses can be transformed using transform functions. By default, Angular applies these transformations:

Request transformations:

  • if the data property of the request config object contains an object, serialize it into JSON format. Response transformations:

  • if XSRF prefix is detected, strip it (see Security Considerations section below)

  • if json response is detected, deserialize it using a JSON parser

So no need to set a data type it is done automatically

5 Comments

The problem is that the server responds with HTML/XML/JSON depending on dataType. The URL is always the same.
@Paul are you talking about the Accept header.
I am talking about Content-type in request
@Paul so the server reads the request content type to determine the response type?
@Paul FYI jQuery sets the Accept header when you set dataType to 'json' not the Content-Type header
4

I had the same problem, responseType:'json' solved the issue

You can use responseType:'json' instead of dataType:'json'

var promise = $http({
            method: 'POST',
            url: 'somewhere.xyz',
            responseType:'json'

        });

For further reference https://docs.angularjs.org/api/ng/service/$http#methods_jsonp

1 Comment

In docs.angularjs.org/api/ng/service/$http#usage reponseType directs you to developer.mozilla.org/en-US/docs/Web/API/… which makes it sound like an override of the server Content-Type with the caution: "When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible to that format. If the server returns data that is not compatible to the responseType that was set, the value of response will be null."

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.