PHP JSON响应包含HTML标头 [英] PHP JSON response contains HTML headers

查看:102
本文介绍了PHP JSON响应包含HTML标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,我试图编写一个PHP页面,该页面将一些JSON返回给Jquery AJAX调用.问题在于,尽管将内容类型设置为application/json,但响应似乎总是包含HTML标头.

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.

这是PHP代码:

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

然后用Java语言编写:

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!');
        }


    });

响应似乎总是这样的:

<!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

根据响应标头,它肯定认为它是JSON:

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

每当我运行代码时,它就会警告错误!".每次都会被解雇,这是可以理解的... 任何人都知道为什么将HTML包含在响应中?

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?

推荐答案

好,我找到了自己的答案,好像我在php.ini文件中打开了tidyhtml,并且有一个

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!

这篇关于PHP JSON响应包含HTML标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆