PHP JSON response contains HTML headers

I have a strange problem when I try to write a PHP page that returns some JSON on a jQuery AJAX call. The problems are that although the / json application is used for the content type, the answer 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:

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

The answer always looks something like this:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <title></title> </head> <body> {"aaData":[["2007-08-01","91109507","Invoice","10.000000","AUD"],["2007-08-02","91110103","Invoice","5.000000","AUD"],["2007-08-02","91110122","Invoice","305.000000","AUD"],["2007-08-02","91110129","Invoice","320.000000","AUD"],["2007-08-03","91111146","Credit for Returns","10.000000","AUD"],["2007-08-06","91111895","Credit for Returns","320.000000","AUD"],["2007-09-03","91128486","Credit Memo","5.000000","AUD"],["2007-09-03","91128487","Credit etc, etc 

And according to the response header, he certainly considers this 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, it warns "Error!". gets fired every time, which is understandable ... Does anyone have any idea why HTML is included in the response?

+6
json content-type jquery php
source share
5 answers

Ok, I found my own answer, it looks like tidyhtml is included inside my PHP.ini file and had

 ob_start("ob_tidyhandler"); 

inside one of my global packages. Commented on this and everything works fine. Thank you for your time!

+2
source share

The header() call actually has nothing to do with returning the HTML code in response.

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

So the line of code

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

does its job correctly because the response contains the correct content type:

 Content-Type: application/json 

So what happened? You probably have code that executes before code associated with json. You should send only json encoded message in your response without any HTML tags and end the script with exit or die . Try to find code that sends HTML tags and puts your code in front of it.

+5
source share

Have you tried commenting out the entire "header (...)" - part? He must work without him. The AJAX call gets all the outputs of the PHP program, including the header.

0
source share

I feel that part of your code emits HTML DTD and the head part automatically for all responses from php codebase. Do you use frames? If so, which one? The fact that json.txt is working indicates that nothing is happening with js, the browser, or any proxies between them.

I suggest you debug the php code stream to see how this part is added to the html response.

0
source share

It is likely that something is publishing headlines before you do it. Can you provide more php code for this? Remember that one empty space outside php tags leads to the output of headers (default is http by default).

0
source share

All Articles