Javascript Can';t在本地服务器上运行xmlHttp.responseXML
这几天我在学习AJAX和XML。最近我遇到了一个愚蠢的问题 我试图构建一个简单的程序,在Javascript Can';t在本地服务器上运行xmlHttp.responseXML,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,这几天我在学习AJAX和XML。最近我遇到了一个愚蠢的问题 我试图构建一个简单的程序,在中显示我输入到输入框中的所有内容。 由于某种原因,当我尝试使用.responseXML属性时,我的程序将无法运行。请注意,当我使用.responseText时,一切正常 我的html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans
中显示我输入到输入框中的所有内容。
由于某种原因,当我尝试使用.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>';
?>
// 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');
}
}
}
我的js代码:
<?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>';
?>
// 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');
}
}
}
//JavaScript文档
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
xmlHttp=false;
}
}否则{
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
xmlHttp=false;
}
}
如果(!xmlHttp){
警报(“无法创建对象”);
}否则{
返回xmlHttp;
}
}
函数过程(){
var x=xmlHttp.readyState
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4){
food=encodeURIComponent(document.getElementById(“userInput”).value)
open(“GET”,“foodstore.php?food=“+food,true”);
x=xmlHttp.readyState;
onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}否则{
setTimeout('process()',1000);
}
}
函数handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var xmlResponse=xmlHttp.responseXML;
root=xmlResponse.documentElement;
警报(root.firstchild);
//message=root.firstChild.data;
document.getElementById(“underInput”).innerHTML=''+xmlResponse+'';
setTimeout('process()',1000);
}否则{
警报(“不工作”);
}
}
}
感谢帮助者。您尝试过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 );
});
}
好吧,我想我找到了窃听器。 我输入了php文件(将响应发送到html)的url,并出现以下错误: “此页面包含以下错误: 第6列第2行出错:仅允许在文档开头使用XML声明“
我删除了PHP顶部的空白之后,我收到了一个新的错误,在我修复了新的错误之后,一切都变得很有魅力了…非常感谢
在handleServerResponse()中,尝试执行以下操作:console.log(xmlHttp.responseXML);告诉我们控制台是什么