PHP headers with AJAX

Posted on Friday July 22, 2011 / by Eric Potvin

When creating an Ajax request you might have to set the headers to provide the correct information to be sent to the browser.

Here's the most common way to force the headers when executing an AJAX request:

Common headers

header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

For jSon (JavaScript Object Nation)

header('Content-type: application/json');

For XML (Extensible Markup Language)

header('Content-Type: text/xml');

For HTML (Hypertext Markup Language)

header('Content-Type: text/html');