Home » Php » jquery – PHP JSON response contains HTML headers

jquery – PHP JSON response contains HTML headers

Posted by: admin July 12, 2020 Leave a comment


I’ve got a strange problem where I’m trying to write a PHP page that returns some JSON to a Jquery AJAX call. Problems is that despite setting the content type to application/json, the response always seems to include the HTML header.

Here’s the PHP code:

// some code that generates an array
header("Content-type: application/json");
echo json_encode($return);

Then in Javascript:

        url: '/VAPHP/services/datatable.php',
        dataType: 'json',
                type: 'invoices'
        success: function(data)
            // show a message saying it's been sent!
        error: function(XMLHttpRequest, textStatus, errorThrown) {


The response always seems to be something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
for Returns","10.000000","AUD"],["2007-08-06","91111895","Credit
for Returns","320.000000","AUD"],["2007-09-03","91128486","Credit
etc, etc

And according to the response header it certainly thinks it’s JSON:

HTTP/1.1 200 OK
Content-Type: application/json
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.3

Whenever I run the code and it alert “Error!” gets fired every time, which is understandable…
Anyone got any ideas why the HTML is being included in the response?

How to&Answers:

Calling header() actually has nothing to do with HTML-code being returned in the response.

header() is used to set HTTP-headers, while HTML-code (<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">) is sent in the body of HTTP response.

So the line of code

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

does his job correctly because response contains correct content type:

Content-Type: application/json

So what’s wrong? Probably you have code that is executed before the code that deals with json. You should send only json encoded message in your response without any HTML tags and terminate the script using exit or die. Try to locate the code that sends HTML tags and put your code before it.


Ok, I found my own answer, looks like I had tidyhtml turned on inside my PHP.ini file, and had a


inside one of my global packages. Commented that out and it all works fine. Thanks for your time everyone!


Have you tried commenting the whole “header(…)”-part? It should work without it. The AJAX-call gets everything the PHP-program outputs, in this case including the header.


I feel somepart of your code is emitting the HTML DTD and the head part automatically for all responses from the php codebase. Are you using a framework ? If so which one ? The fact that the json.txt works is indicative that nothings wrong with js, browser or any proxies in between.

I suggest you debug the php code flow to see where this part is getting added to the html response.


There is probably something posting headers before you do. Can you provide more of the php code for this?Remember that a single white space outside of the php tags forces the output of headers ( http by default).