|
This change, while I think is a good idea in general, will be somewhat
difficult to make use of if the new JSON object replaces the old JSON
object. I, and apparently others, use X-JSON often for error checking, debug messages, status flags, etc.. When your application requires the occasional use of raw HTML and you don't want to encode it all in a JSON message, it is very useful. However, I still use JSON encoded data in the response body because of the size limitations of the X-JSON header. Having X-JSON *replaced* by the evaluated responseText, will make it difficult for when I want to still use the X-JSON header for error/debug/status info, but send the main response body as either plain X-HTML or JSON due to the size limit. To make it more clear, I would have to account for the following possible scenarios on the server side in order to make use of the new feature: 1) If response is HTML then error/debug is X-JSON 2) If response is JSON then error/debug is in response JSON It would be nice if the body JSON didn't replace the X-JSON because then I could always use X-JSON for error/debug regardless of what is in the responseText. This just seems like better separation of data and error/status handling to me. My suggestion is perhaps rather than replacing X-JSON it should be placed inside the response object? e.g. "xhr.json" similar to "xhr.responseText" Thoughts? Colin ----code examples----- Hypothetical JS using my suggestion: onComplete: function(xhr,json){ console.debug(xhr.responseText); //the responseText console.debug(xhr.json); //the evaluated responseText (if "Content-type: application/x-json") console.debug(json); //the X-JSON evaluated header if(json && json.error){ alert(json.error); return; } if(json && json.login){ showLogin(); return; } //do something with xhr.json } Hypothetically on the server-side (PHP): ob_start(); $status = Array(); $json = Array(); if(!$loggedIn){ $status['login'] = 'true'; header('X-JSON: ('.json_encode($status).')'); return; } if( /*handling request using JSON */ ){ if(/*error*/){ $status['error'] = 'Error message!'; } else{ $json['data'] = Array(/* data here */); } }else if( /*handling request with HTML */){ if(/*error*/){ $status['error'] = 'Error message!'; } else{ print '<b>HTML! Yay!</b>'; } } $content = ob_get_clean(); if($status){ header('X-JSON: ('.json_encode($status).')'); } if($json){ header('Content-type: application/x-json'); print '('.json_encode($json).')'; }else{ print $content; } Alexander Presber wrote: Am 22.01.2007 um 13:46 schrieb Dave Crane: --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~--- |
- [Rails-spinoffs] Re: About X-JSON header and evil thing... Ed C.
- [Rails-spinoffs] Re: About X-JSON header and evil ... Mislav Marohnić
- [Rails-spinoffs] Re: About X-JSON header and evil ... Dave Crane
- [Rails-spinoffs] Re: About X-JSON header and e... Kjell Bublitz
- [Rails-spinoffs] Re: About X-JSON header a... Mislav Marohnić
- [Rails-spinoffs] Re: About X-JSON head... Ryan Gahl
- [Rails-spinoffs] Re: About X-JSON... Kjell Bublitz
- [Rails-spinoffs] Re: About X-... Martin Bialasinski
- [Rails-spinoffs] Re: About X-JSON header and evil ... Colin Mollenhour
- [Rails-spinoffs] Re: About X-JSON header and e... Mislav Marohnić
- [Rails-spinoffs] Re: About X-JSON header and evil ... Peter Michaux
- [Rails-spinoffs] Re: About X-JSON header and e... William F. Attwood
- [Rails-spinoffs] Re: About X-JSON header and evil ... Tom Gregory
- [Rails-spinoffs] Re: About X-JSON header and e... Kjell Bublitz
- [Rails-spinoffs] Re: About X-JSON header a... mryhanen
- [Rails-spinoffs] Re: About X-JSON head... Thomas Fuchs
- [Rails-spinoffs] Re: About X-JSON... Thomas Fuchs
- [Rails-spinoffs] Re: About X-JSON... tobie
