galiolio
galiolio

Reputation: 275

How to specify datatype and contentType in $http requests in AngularJS

I'd don't find on the internet how can I pass datatype and contentType to a shortcut request in AngularJS.

Exemple :

$http.get('url',{
                 headers:{'header1':'value'}
})

Second question : Can I send the header with this kind of code ?

beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + Api.getToken());
            }

Upvotes: 0

Views: 274

Answers (1)

Ghulam Mohayudin
Ghulam Mohayudin

Reputation: 1103

You can add a content type like this, but for this you also need to specify data in the request also

 return $http({
               method: 'POST',
               //withCredentials:true,
               headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
               data: data,
               url: yourUrlHere
             });

data can be an empty string but with if you are not adding data it will not set the content-type

For short post method you can do it like this

 $http.post('/someUrl', data, {headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}).then(successCallback, errorCallback);

Upvotes: 1

Related Questions