【问题标题】:Extra content at the end of the document - Ajax文档末尾的额外内容 - Ajax
【发布时间】:2015-08-24 12:37:05
【问题描述】:

当我运行我的代码时,会出现这个错误:

此页面包含以下错误:

第 2 行第 1 列的错误:文档末尾的额外内容

下面是第一个错误之前的页面渲染。

我在两个文件中编写了代码:javascript 和 php,如下所示:

<?php
    header('Content-Type: text/xml');
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

    echo '<response>';
        $food = isset($_GET['food']);
        $foodArray = array('tuna', 'bacon', 'beef');
        if(in_array($food, $foodArray)){
            echo 'We do have'.$food;
        }
        elseif($food==''){
            echo 'Enter a food';
        }else{
            echo 'We dont sell'.$food;
        }
    echo '</response>';
?>

<html>
    <head>
        <script src="index.js"></script>
    </head>
    <body onload="process()">
        <form method ="get" action="index.php"><input type="text" id="userInput"/></form>
        <div id="underInput"></div>
    </body>
</html>

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
    var xmlHttp;

    if(window.ActiveXObject){
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
            xmlHttp = false;
        }
    }else{
        try{
            xmlHttp = new XMLHttpRequest();
        }catch(e){
            xmlHttp = false;
        }
    }

    if(!xmlHttp){
        alert("Error")
    }else{
        return xmlHttp;
    }
}

function process(){
    if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4){
        food = encodeURIComponent(document.getElementById("userInput").value);
        xmlHttp.open("GET", "index.php?food=" + food,true);
        xmlHttp.onreadystatechange = handleServerResponse;
        xmlHttp.send(null);
    }else{
        setTimeout('process()', 1000);
    }
}

function handleServerResponse(){
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
            xmlResponse = xmlHttp.responseXML;
            xmlDocument = xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.dataa;
            document.getElementById("underInput").innerHTML = '<span style="color:blue">' + message + '</span>';
            setTimeout('process()', 1000);
        }else{
            alert("Error1");
        }
    }
}

有人可以帮忙吗?

谢谢

【问题讨论】:

标签: javascript php ajax


【解决方案1】:

这个错误是因为 html 代码。

制作不同的php文件。不要在其中添加html代码

<?php
    header('Content-Type: text/xml');
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

    echo '<response>';
        $food = isset($_GET['food']);
        $foodArray = array('tuna', 'bacon', 'beef');
        if(in_array($food, $foodArray)){
            echo 'We do have'.$food;
        }
        elseif($food==''){
            echo 'Enter a food';
        }else{
            echo 'We dont sell'.$food;
        }
    echo '</response>';
?>

在 html 文件中使用keyup 或任何其他不使用 onload 函数的函数进行更改

<html>
    <head>
        <script src="xml.js"></script>
    </head>
    <body>
        <form method ="get" action="test.php">
            <input type="text" id="userInput" onkeyup = "process();"/>
        </form>
        <div id="underInput"></div>
    </body>
</html>

codepad

【讨论】:

  • 当我给任何食物小费时,它应该立即回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多