简体   繁体   English

脚本第1行出现php错误“标头已设置”

[英]php error “headers already set” on line 1 of script

I have a php script that is supposed to generate an xml file for an ajax call. 我有一个PHP脚本,应该为ajax调用生成一个xml文件。 It is producing this error message: 它产生此错误消息:

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 警告:无法修改标头信息-已发送的标头(输出始于/ home / ocp / public_html / sites / all / modules /中的/home/ocp/public_html/sites/all/modules/bookDisplay/getVersions.php:1)第2行上的bookDisplay / getVersions.php

The strange thing is that the start of the php script looks like this: 奇怪的是,PHP脚本的开始看起来像这样:

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

So there's nowhere on line 1 for any header to be sent before I try to send them again on line 2! 因此,在第1行上没有任何要发送的标头,然后再尝试在第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. 我发现的所有类似问题的讨论都涉及额外的空格,但是我已经进行了三遍检查,没有多余的行或空格。

The script is called from a jquery ajax() function that looks like this: 该脚本是通过如下所示的jquery ajax()函数调用的:

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

    }
  });
}

Is this jquery function somehow sending headers that conflict with the headers sent in the php file? 这个jQuery函数是否以某种方式发送与PHP文件中发送的标题冲突的标题? I've tried removing the "dataType" declaration, and that does get rid of the error message. 我尝试删除“ dataType”声明,但这确实消除了错误消息。 But then the response comes back as text/html, not as xml. 但是然后响应以text / html而不是以xml的形式返回。 How can I get a proper xml response to the ajax call without the "headers already sent" error? 如何获得对ajax调用的正确xml响应,而不会出现“标题已发送”错误?

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 ?> . 确保在标头函数之前没有代码/ html,甚至没有空格/缩进,并且在第一个打开的php标签<?php以及结束标签?>之前没有任何内容。

Also be aware of BOM (Byte Order Mark) there might exist there possibly. 另外,请注意那里可能存在BOM(字节顺序标记)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM