php - Can I change the CodeIgniter show_error function to return a JSON object? -


i'm developing full ajax app ci , want show_error function return json data, debug trough own js code. there native or simple ways or need change show_error function? if needed, can overridden?

well maybe solution isn't entirely useful if looking return complex error objects arrays (although could, you'd have parse json on client) here go:

just use http

why? well, can pass second parameter it, happens http response code, happens cool, cause allows make application http-aware , works amazingly client-side ajax requests.

what want first define kind of errors can happen on backend, there list of http error codes here.

http error codes

most you'll use error codes in 200, 400 , 500 ranges. when hit server on web browser receive 200 http response code mean went fine.

have seen "internal server eror" messages? 500 http response codes. , means that, error server. one? depends how categorize them, there's set of errors on 500 range, if don't want hassle use 500 generic error code response.

the other range 400. error users, example if go url inside server , not exist you'd famous 404 not found, 400 generic error code meaning client (in case, browser) requested request invalid, in case of 404 specifically, resource requested not found, client error because supposed know resources available on server.

how in codeigniter

it extremely simple actually. if see show_error() reference on documentation states method receives first parameter error message, , second, optional, receives error code. error code? http codes talked before, so:

show_error('howdy, debug message', 500); 

would send 500 http response code client, including message.

how catch in ajax

considering using jquery do:

$.ajax({     type: 'post',     url : example.com/resource,     data: $("#some-form").serialize(),     datatype: 'json',     success : function(data, textstatus, req) {         //do data json object returned php     },     error: function(req, textstatus, errorthrown) {         //this going happen when send different 200 ok http         alert('ooops, happened: ' + textstatus + ' ' +errorthrown);     } }); 

if using other toolkit or dom object directly can still catch since xmlhttprequest objects , chances toolkit has callback either http error response or success response.

why care?

because follows standards, easier debug, delegate work show_error() helper there reason , importantly because cool kids using it.

cool, wait, don't see custom error message nowhere!

that right, because when catch request in error callback of jquery generic error description , code "internal server error" , 500 respectively, however, still got pretty html response custom debug message, see use sort of developer tool firefox or chrome. example, if use google chrome can open developer tools:

go network tab , you'll see http request, click on name

network details

you'll see details , custom error message usual ci template, html returned message inside request

request response

finally if want dig further , debug sent php/web server client go headers option

headers info

disclaimer: screenshots not taken production server :)


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -