16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Dependency Injection and Services<br />

};<br />

.error(function(data, status, headers, config) {<br />

switch(status) {<br />

case 401: {<br />

$scope.message = "You must be authenticated!"<br />

break;<br />

}<br />

case 500: {<br />

$scope.message = "Something went wrong!";<br />

break;<br />

}<br />

}<br />

console.log(data, status);<br />

});<br />

retrieveCars();<br />

});<br />

Headers<br />

By default, the framework adds some HTTP headers to all of the requests, and other<br />

headers only to the POST and PUT methods.<br />

The headers are shown in the following code, and we can check them out by<br />

analyzing the $http.defaults.headers configuration object:<br />

{<br />

}<br />

"common":{"Accept":"application/json, text/plain, */*"},<br />

"post":{"Content-Type":"application/json;charset=utf-8"},<br />

"put":{"Content-Type":"application/json;charset=utf-8"},<br />

"patch":{"Content-Type":"application/json;charset=utf-8"}<br />

In case you want to add a specific header or even change the defaults, you can<br />

use the run function of the Module API, which is very useful in initializing<br />

the application:<br />

run.js<br />

parking.run(function ($http) {<br />

$http.defaults.headers.common.Accept = "application/json";<br />

});<br />

[ 84 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!