doude1917 2013-05-02 11:29
浏览 75

未定义xmlDocumentElement

Here is my code: HTML:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="foodstore.js"></script>
    </head>
    <body onLoad="process()">
        <h3>WELCOME to our online food store</h3>
        Please type what you want to order:
        <input type="text" id="userinput" ></input>
        <div id="underInput"></div>
    </body>
</html>

PHP

 <?php
header('Content-Type: text/xml');
echo"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>";
echo '<response>';
            $food = $_GET['food'];
            $foodArray = array('tuna','bacon','beef','loaf','mutton');
            if(in_array($food,$foodArray))
                echo 'We do have'.' '.$food;
            elseif($food=='')
                echo 'type something dude!';
            else
                echo 'Sorry we dont sell'.' '.$food;
echo '</response>';
?>

JS

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
    {
        var xmlHttp;

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

        if(!xmlHttp)
            {
                alert('cannot connect to database!');
            }
        else
            {
                return xmlHttp;
            }
    }

function process()
    {
        if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
            {
                food = encodeURIComponent(document.getElementById("userinput").value);
                xmlHttp.open("GET", "ajax.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;
                        xmlDocumnetElement = xmlResponse.documentElement;
                        message = xmlDocumentElement.firstChild.data;
                        document.getElementById("underInput").innerHtml = "<span style='color:blue'" + message + "</span>";
                        setTimeOut('process()',1000);
                    }
            }
        else
            {
                alert('something went wrong');
            }
    }

Everything seems to be fine to me but I still keep getting the error 'xmlDocumentElement is not defined'. Seems like this is an error due to some parsing issues within javascript and php.. Please help me fix this error. thanks

  • 写回答

1条回答 默认 最新

  • dongseshu0698 2013-05-02 12:37
    关注

    try to use

    window.setTimeOut('process', 1000);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用