JSON返回HTML结果

I'm using dataTable and I'm having the Json parse error. Here is my code:

jQuery(function($){
         $('#allusers').dataTable({
             "sAjaxSource": document.URL+"/allusers.php"
         });

        $.ajax({
            url: document.URL+"/allusers.php",
            success: function(data){
                alert (data);
            }
        });
    });

So inserted the $.ajax part so that I could see the data being returned. (Well I could also see it using firebug, just wanna make sure) and the response that I am getting is the HTML code instead of the JSON object.

here is the snippet of allusers.php:

$dataArr['aaData'] = Array();
$res = $mysqli->query($query);
$numrows = $res->num_rows;
$output = array('iTotalRecords' => $numrows, "iTotalDisplayRecords" => 10, "aaData" => array() );
if ($res = $mysqli->query($query)){
    while($row = $res->fetch_array(MYSQLI_ASSOC)){
        $r = Array();
        foreach($row as $key=>$value){
            $r[] = $value;
        }
        $output['aaData'][] = $r;
    } 
} else 
    die(mysql_errno());
$output['err'] = 'hello';
header('Content-Type: application/json');
echo json_encode($output);
exit();

I already tried var_dump($r) to check the contents of $output and I don't see a problem with it.

So the result that I'm getting is:

<!DOCTYPE html>
<html class="home">
<head>
<title>User List</title>
...

it basically returns the html content of the whole page. Is the problem within the javascript or php? Thanks in advance for the help!

Not sure if your PHP framework respects requested datatypes, but you could start by requesting JSON specifically. Either specify:

$.ajax({
        url: document.URL+"/allusers.php",
        dataType: "application/json",
        success: function(data){
            alert (data);
        }
    });

or use the jquery json shortcut:

$.getJSON("url", data, callbackFuntion);

The default for the $.ajax() method is to intelligently guess the output, so it seems weird it doesn't pick up on your content-type.

My last idea would be that you specified php headers and footers globally somehow, so they are added to your output?

I've already fixed this problem. Since I'm using a MVC, I put the function in the controller then just added a code like this:

if (isset($_GET["var"]))
    function();

also I removed the header('Content-Type: application/json');

I don't know why, but when I removed that it worked. Thanks all for pitching in your ideas. :)