简体   繁体   中英

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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