dpca31461 2014-07-17 09:24
浏览 57

我的代码在条件失败(xmlHttp.status == 200),我一直被警告'出错了'

This is my Html code ,which calls on process() function with foodstore.js as it's attached javascript file

<script type="text/javascript" src="foodstore.js"> </script>

</head>
<body onload="process()">
    <h3>The Chuff Bucket</h3>
    Enter the food you would like to order:
    <input type="text" id="userInput">
    <div id="underInput"> </div>

   </body>
   </head>

This is my php code

  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','ham');
    if(in_array($food,$foodArray))
            echo 'We do have '.$food.'!';
            elseif($food=='')
                    echo 'Enter a food you idiot';
            else
                    echo 'Sorry punk we dont sell no ' .$food.'!';

        echo '</response>';


      ?>

This is my java script code

 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("cant create that object hoss!");
     else
         return xmlHttp;
      }

     function process(){   
     if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
     {

     food=encodeURIComponent(document.getElementById("userInput").value );
      xmlHttp.open("GET", "foodstore.php?food="+food, true);
      xmlHttp.onreadystatechange = handleServerResponse;

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

          +message + '</span>';
           setTimeout('process()', 1000);
          }
      else{
        alert('Something went wrong!');
        }
      }
      }

I feel the error is mostly to do with "handleServerResponse" function ,Any help would be highly appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入