javascript - 文档末尾的额外内容 - Ajax

标签 javascript php ajax

当我运行代码时出现此错误:

This page contains the following errors:

error on line 2 at column 1: Extra content at the end of the document

Below is a rendering of the page up to the first error.

我在两个文件中编写了代码: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");
        }
    }
}

有人可以帮忙吗?

谢谢

最佳答案

错误是由于 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>';
?>

使用keyup或任何其他函数更改html文件,不要使用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

关于javascript - 文档末尾的额外内容 - Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182517/

相关文章:

javascript - 在 Xamarin 中从 C# 对象调用 JavaScript

php - 如何创建插入表,其中部分数据来自一张表,其余数据已填充

php - 自定义 404 错误页面——仅适用于 PHP 页面

javascript - 单击变量 id 的下拉菜单

javascript - 如何检测何时使用 JavaScript 动态注入(inject)多个样式表

在 PDF 中选中 PHP mPDF 复选框

jquery - 使用jquery和ajax添加好友功能

php - 从 SQL DB 而不是聊天应用程序的文件进行轮询会提高性能吗?

javascript - JSONP 请求永远不会调用成功回调

javascript - AngularJS 中的数组未获取字符串值