AJAX Request + JSON Responses

X-JSON HTTP Header

You can send an Ajax Request, and in the response, add an HTTP status type of "X-JSON" which will be automatically available as headerJSON in the transport object.

The variable type will be an Object.

Result:

To get a string from the object, treat it as you would any other.

Object.toJSON(transport.responseJSON);

Result:

JSON String in HTTP Response Text

Where the response text is a JSON string, it will still show up as a string. You can use Prototype's functions normally after that.

transport.responseText

Result:

Since the responseText comes back as a JSON String, you can convert it easily to an object.

transport.responseText.evalJSON(true);

Result:

requestHeaders: { Accept: 'application/json' }

Ajax's Request method has an option to add which headers to request, but I'm having a hard time seeing how that affects anything.

It adds an HTTP header along with the request asking for that application type back, but it doesn't affect the handling of the response text (as near as I can tell).

You can see the request server-side in PHP with $_SERVER['HTTP_ACCEPT']