2014-05-23 52 views
0

我学习AJAX和XML这几天上运行xmlHttp.responseXML。最近我遇到了一个愚蠢的问题。无法在本地服务器

我尝试建立一个简单的程序,会告诉我在<div>一切,我输入到输入框中。 出于某种原因,当我尝试使用.responseXML物业我的程序将无法运行。请注意,当我使用.responseText一切工作正常。

我的html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Untitled Document</title> 
     <script type="text/javascript" src="foodstore.js"></script> 
    </head> 
    <h3> the chuff bucket </h3> 
    <body onload="process()"> 

     <input type="text" id="userInput"/> 
     <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']; 
echo $food; 
echo '</response>'; 
?> 

我的js代码:

// JavaScript Document 
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 object"); 
    }else{ 
     return xmlHttp; 
    } 
} 



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

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

    }else{ 
     setTimeout('process()',1000); 
    } 
} 


function handleServerResponse(){ 

    if(xmlHttp.readyState==4){ 
     if(xmlHttp.status==200){ 

     var xmlResponse= xmlHttp.responseXML; 
      root = xmlResponse.documentElement; 
      alert(root.firstchild); 
      //message= root.firstChild.data; 

      document.getElementById("underInput").innerHTML= '<span  style="color:blue">' + xmlResponse + '</span>'; 
      setTimeout('process()', 1000); 
     }else{ 
      alert('not working'); 
     } 
    } 
} 

感谢帮手。

回答

0

你有没有尝试过使用jQuery?这可能更容易做到。你可以指定数据类型

function ajaxJQueryRequest(url,afterReqFunction){ 
    var request = $.ajax({ 
     url: url, 
     type: "GET", 
     crossDomain: true, 
     dataType: "xml" 
     }); 
    request.done(function(msg) { 
      afterReqFunction(msg); 
     }); 
    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
     }); 
} 
+0

谢谢,但我想这样做我的方式,因为我想一步一步学习... – misha312

+0

在handleServerResponse()尝试做: console.log(xmlHttp.responseXML);并告诉我们控制台是什么 – user3667931

0

好吧我想我找到了这个bug。 我进入了php文件的URL(即发送到HTML的响应),并得到了以下错误: “此页包含以下错误:

第2行的错误,在第6列:XML声明只允许文档”

我删除了在我收到一个新的错误我的PHP上方的空白后,在我的固定新的错误一切的开始工作就像一个魅力...非常感谢!

相关问题