1

I have a simple ajax request:

$.post('server.php',data, function (json) {console.log(json)},'json');

I've set it up so that jQuery is expecting json as specified by the last dataType setting.

Related question: Is dataType another name for responseType? See http://msdn.microsoft.com/en-us/library/windows/apps/hh871381.aspx

Now my real question. server.php sometimes returns json, other times html, and other times xml, and will set the appropriate header when rendering the response.

Before rendering the response, however, the server needs to determine what type of data to provide. I wish to base the response upon the dataType and/or responseType header it receives from the client. How do I read this header within PHP?

user1032531
  • 24,767
  • 68
  • 217
  • 387
  • `var dtype = "json" || "xml";` and you can try using here at the dataType place of `$.post()`: `console.log(json)},dtype);`. its a guess you can try this to see. – Jai Feb 18 '14 at 13:14
  • I think this might interest you: http://stackoverflow.com/questions/2579254/php-does-serverhttp-x-requested-with-exist-or-not – MonkeyZeus Feb 18 '14 at 13:33
  • @MonkeyZeus. Yes, it did interest me. They recommend not trying to do so, and POST or GET a flag. One of the stated concerns was being able to spoof the server using cURL, but that is no different than GET/POST flag. My only concern is interoperability with various browsers. Do you recommend not trying to do so? Thanks – user1032531 Feb 18 '14 at 13:42
  • Well that is up to you and your specific situation. Are you developing a public API or will this only be used internally within your own application? – MonkeyZeus Feb 18 '14 at 13:51
  • Also, are you using a PHP framework? – MonkeyZeus Feb 18 '14 at 13:51
  • I am not sure what `responseType` is exactly but in a nutshell `dataType` is the type of data which your AJAX is waiting to receive and potentially act upon. For instance if you tell it to listen for JSON from the `server.php` then upon receiving a response jQuery will automatically send it to an interpreter which will create a JS object/array depending on the info within the JSON string. – MonkeyZeus Feb 18 '14 at 13:56
  • @MonkeyZeus. Internal application. No framework. – user1032531 Feb 20 '14 at 23:14

1 Answers1

1

The XMLHttpRequest.responseType attribute, is no information that is in any form send to the server. Therefore you cannot use this to have your server's php code determine the requested content-type.

However there is a HTTP Request Header field named ACCEPT (see mdn), which can be set using the XMLHttpRequest.setRequestHeader() (see mdn again). With it you can attribute to the request some information, accessible to your php code via $_SERVER['HTTP_ACCEPT'].

On client-side, in place of (or in addition to) setting XMLHttpRequest.responseType you can do this

// create XHR Object
var xhr =  new XMLHttpRequest();

// open request
xhr.open(<url to request>, "POST");

// set the responseType to json (which only provokes
// that the returned data is automatically interpreted
// by the browser as being json. saving you from manually
// doing a JSON.parse(xhr.responseText); 
xhr.responseType='json';

// set the request HTTP ACCEPT header to be the MIME of JSON 
// that is "application/json"
xhr.setRequestHeader("ACCEPT","application/json");

//[... setup the callbacks for handling the resonses or failures]

//send the request
xhr.send();

On server-side, you might then do something along the lines of

if($_SERVER['HTTP_ACCEPT'] === 'application/json')
{
    header('Content-Type: application/json');
    echo '<json-string>';
    die(0);
}
else
{
    // do some stuff for other Request that do 
    // not want explicitly json.
}
humanityANDpeace
  • 4,350
  • 3
  • 37
  • 63
  • This is a good explanation that, for me, clears up uncertainty about xhr.responseType. The MDN docs, and others, are vague about which mechanisms/protocols base their behaviour on responseType. MDN says: "A string which specifies what type of data the response contains.", and "It also lets the author change the response type". What??? It's the server that creates the response and decides on the Content-type. Thank you @humanityANDpeace for clarifying that responseType is a directive to the browser. – Chris Robinson Jun 02 '22 at 10:44