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 />

The following code shows an object literal after being converted into JSON through<br />

the JSON.stringify function:<br />

{<br />

}<br />

"plate": "6MBV006",<br />

"color": "Blue",<br />

"entrance": "2013-12-09T23:46:15.186Z"<br />

Also, the framework provides a built-in function called angular.toJson that<br />

does the same job of converting an object literal to JSON. To perform the other<br />

way round, we can use the angular.fromJson function, which is equivalent<br />

to the JSON.parse function.<br />

To change any entity that already exists, we can rely on the PUT method, using the<br />

same concepts used by the POST method.<br />

PUT /cars/1 HTTP/1.1<br />

Finally, the DELETE method is responsible for deleting the existing entities.<br />

DELETE /cars/1 HTTP/1.1<br />

Another important thing to keep in mind is the status code that is returned in each<br />

response. It determines the result of the entire operation and must allow us to<br />

implement the correct application behavior in case there is an error.<br />

There are many status codes available in the HTTP protocol; however, we should<br />

understand and handle at least the following:<br />

• 200 OK<br />

• 400 Bad Request<br />

• 401 Unauthorized<br />

• 403 Forbidden<br />

• 404 Not Found<br />

• 500 Internal Server Error<br />

In case of an error, the response must bring the associated message, explaining<br />

what's happening and allowing the developers to handle it.<br />

There are many other concepts involving REST. This is just a brief overview and<br />

as it is not the purpose of this book, you can consider studying it from a more<br />

specific source.<br />

[ 78 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!