8

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:

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


    });

The response always seems to be 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 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?

9
  • 2
    How do you inspect the response? Commented Jan 25, 2011 at 11:23
  • 4
    Probably your application does that elsewhere, and since you have output buffering enabled you do not get the 'headers already sent' error. Commented Jan 25, 2011 at 11:24
  • 1
    Are you using a framework or is it a stand-alone PHP file? Commented Jan 25, 2011 at 11:58
  • Checking the response with Firebug Commented Jan 25, 2011 at 11:59
  • Not using any PHp frameworks, uses a few classes I've written, but they don't do anything exciting except call a couple of SQL queries. Commented Jan 25, 2011 at 12:00

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

THIS is what I had wrong on my php file. I had a doctype tag before the initial php script that sends back json data. Be sure to put all html tags under php scripts that will determine if you will be sending data out or outputting the html bellow instead.
2

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

ob_start("ob_tidyhandler"); 

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

Comments

0

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.

3 Comments

Only difference that makes is this header: HTTP/1.1 200 OK Content-Length: 3464 Content-Type: text/html Server: Microsoft-IIS/7.5 X-Powered-By: PHP/5.3.3
If firebug shows that the data coming from the server has the HTML-header, and that's your whole PHP file, it's one of the 2 functions producing the HTML header. Have you tried with a static mock-JSON-file on the server side? Where you're 100% of what data gets sent?
Ok, with a json.txt file, containing just straight JSON it works fine
0

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.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.