X-JSON header or application/json MimeType

181 views
Skip to first unread message

Moisés Gramary Barbosa

unread,
Nov 19, 2009, 12:39:23 PM11/19/09
to Prototype & script.aculo.us
Hello everyone, firstly thank you very much.

I have the following questions:

Since I work with prototypejs AJAX requests to return, I always return
calls as application/json parsing the JSON object in the response, so
I had to always use something like the following to use the object:

var json = transport.responseText.evalJSON();

However in the documentation of prototype, to use the JSON object
directly, they recommend using, for example:

var json = transport.headerJSON;

To proceed in this manner is necessary to write the header X-JSON
object with the contents of the JSON...

My questions are:

What improvements can I do with this method?
PrototypeJS could not detect the response MimeType, as application/
json, and interpret it the same?
And last ... If I refactor my script to use the X-JSON header, the
response of the request should be empty?

Sorry for my English

T.J. Crowder

unread,
Nov 20, 2009, 7:05:09 AM11/20/09
to Prototype & script.aculo.us
Hi,

> Since I work with prototypejs AJAX requests to return, I always return
> calls as application/json parsing the JSON object in the response, so
> I had to always use something like the following to use the object:
>
> var json = transport.responseText.evalJSON();

There's no need to do that. Just use the transport.responseJSON
property. Prototype detects application/json responses, automatically
evaluating the JSON and populating that property with it. Calling
transport.responseText.evalJSON() just duplicates that effort.

> However in the documentation of prototype, to use the JSON object
> directly, they recommend using, for example:
>
> var json = transport.headerJSON;
>
> To proceed in this manner is necessary to write the header X-JSON
> object with the contents of the JSON...

This is just a useful way to send *additional* data related to the
response on top of the primary response. So for instance, you might
respond with text/html data but include an X-JSON header to supply
further information about it.

For what you're doing (an Ajax request specifically to get the data),
I don't see any advantage; just keep doing what you're doing server-
side and start using the responseJSON property.

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

On Nov 19, 5:39 pm, Moisés Gramary Barbosa <moigram...@gmail.com>
wrote:

Moisés Gramary Barbosa

unread,
Nov 20, 2009, 8:32:16 AM11/20/09
to Prototype & script.aculo.us
Ohhh, I had no idea this functionality.

I just tested and works perfectly.

Many thanks for your help and explanations!
Reply all
Reply to author
Forward
0 new messages