PHP的错误"头已经设置"在脚本的第1行 [英] php error "headers already set" on line 1 of script

查看:144
本文介绍了PHP的错误"头已经设置"在脚本的第1行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,应该生成一个Ajax调用的XML文件。它是生产此错误消息:

I have a php script that is supposed to generate an xml file for an ajax call. It is producing this error message:

警告:无法修改标题信息 - 在/ home头已经发出(输出开始/home/ocp/public_html/sites/all/modules/bookDisplay/getVersions.php:1)/ OCP /的public_html /网站/任何/modules/bookDisplay/getVersions.php第2行

Warning: Cannot modify header information - headers already sent by (output started at /home/ocp/public_html/sites/all/modules/bookDisplay/getVersions.php:1) in /home/ocp/public_html/sites/all/modules/bookDisplay/getVersions.php on line 2

但奇怪的是,PHP脚本的开头是这样的:

The strange thing is that the start of the php script looks like this:

<?php
  header('content-type:application/xml;charset=utf-8');

因此​​,有无处1号线的任何标题之前,我尝试将它们重新发送第2行发送!所有我发现有关类似问题的讨论中谈多余的空格,但我已经检查和三重检查,没有任何多余的线条或空格。

So there's nowhere on line 1 for any header to be sent before I try to send them again on line 2! All of the discussions I've found about similar problems talk about extra whitespace, but I've checked and triple-checked and there are no extra lines or spaces.

该脚本是从jQuery的阿贾克斯()函数,它看起来像这家名为:

The script is called from a jquery ajax() function that looks like this:

function getVersions(){
  var bookSelected = $.data(document.body, 'book');
  $.ajax({
    cache:"false",
    type: "GET",
    dataType: "xml",
    url: "sites/all/modules/bookDisplay/getVersions.php",
    data: "book=" + bookSelected,
    success:function(xml){

    }
  });
}

时的这个jQuery功能某种程度上发送头与PHP文件发送的头冲突?我试图消除数据类型的声明,这确实摆脱了错误信息。但随后的响应返回为text / html,而不是XML。我怎样才能得到一个正确的XML响应Ajax调用不带已经发送了头的错误?

Is this jquery function somehow sending headers that conflict with the headers sent in the php file? I've tried removing the "dataType" declaration, and that does get rid of the error message. But then the response comes back as text/html, not as xml. How can I get a proper xml response to the ajax call without the "headers already sent" error?

推荐答案

请注意,头要发送的 其他任何操作之前的。确保有头功能之前没有code / HTML,甚至空间/压痕并没有什么第一开口PHP标签之前&LT; PHP 以及作为结束标记&GT;

Note that headers should be sent before anything else. Make sure that there is no code/html or even space/indentation before the header function and there is nothing before the first opening php tag <?php as well as ending tag ?>.

另外要注意的 BOM(字节顺序标记)可能存在有可能的。

Also be aware of BOM (Byte Order Mark) there might exist there possibly.

这篇关于PHP的错误&QUOT;头已经设置&QUOT;在脚本的第1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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