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:

  
On Saturday 20 January 2007 15:22, Kjell Bublitz wrote:
    
Secondly, there is no point for X-JSON to be a status message carrier
or "just for simple stuff". Drop it entirely. It's is unnecessary.

Most people will have an own error format / object notation which  
they
transport in the responseText as a replacement for the expected
content and act on it. (eg include a "status" key)

      
My reading of the X-JSON header was that it's useful in cases where  
you want
to pass some secondary data back with a response. The practice of
'piggy-backing' data in this way isn't to everybody's taste, but it  
can be a
useful way of avoiding extra traffic.

Rails & Prototype both have a strong bent towards delivering  
fragments of HTML
rather than data in the response. In this case, If you're sending back
content, I don't see how to easily embed secondary data inside the  
main
response body, other than stuffing it into a hidden DIV, which  
loses several
hundred points for elegance. Sure, if you're delivering data  
anyway, X-JSON
isn't particularly useful.
    

Just want to support that opinion. We are using the X-JSON header  
exactly for that.

Alex

  
On Monday 22 January 2007 12:42, Mislav wrote:
    
Will be done:
http://dev.rubyonrails.org/ticket/7295
      
Just saw this come in on the list - sounds like a good suggestion  
to me.



Just my $0.02

Dave
-- 
----------------------
Author
Ajax in Action http://manning.com/crane
Ajax in Practice http://manning.com/crane2
Prototype & Scriptaculous Quickly http://manning.com/crane3

    




  

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

Reply via email to