Pages

Thursday 26 January 2012

HTTP Status Code usage

Status code specially using in Ajax calls.

Using http status code is now getting in practice and should be used to take advantages of these codes instead of handling them manually.

Making it simple by categorizing the response either success or failure where there responding codes are 200 and 400.

In context of rails there can be two ways to handle the response.

  • First one the bad one

render :text => "Its working" and return

Now on rendering page we need to put string comparison to find out that either its success or failure response.

  • Second one the good one
render :text => "Intelligent Text", :status => 200 and return 

Now here we don't need to make any comparison of responded text the status code will automatically map to relevant response.

like 

ajax:success( // do success stuff)
ajax:failure(  // do failure stuff)

That's it the relevant function will be invoked on basis of relevant status code.

Good to use status code.

:)

No comments:

Post a Comment