XMLHttpRequest.responseXML从Ajax到.php页面返回NULL
XMLHttpRequest.responseXML returns NULL from Ajax to .php page
我有一个搜索框,人们可以在其中填写ID。如果他们按下旁边的按钮,我会向php文件发出ajax请求,该文件会获取ID,在DB中查找相关信息,并应以XML页面的形式返回。
loadContact.php
<?php
header("Content-type: application/xml");
include('../classes/php.php');
$contactID = $_POST["contactID"];
$mItemArray = getItemFromID($contactID);
echo "
<?xml version='"1.0'" encoding='"utf-8'" ?>
<app naam='"send-to-work'" id='"50029154'">
<contact id='"" . $contactID . "'">
<team>" . $mItemArray['Team'] . "</team>
<role>" . $mItemArray['Role'] . "</role>
<name>" . $mItemArray['Name'] . "</name>
<firstname>" . $mItemArray['Fname'] . "</firstname>
<phone>" . $mItemArray['Phone'] . "</phone>
<email>" . $mItemArray['Email'] . "</email>
</contact>
</app>
";
?>
js.js
function loadContact()
{
var xmlhttp;
var x;
var contactID = $("input#id").val();
if (contactID != "" && contactID == parseInt(contactID)){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==404)
{
alert("PAGE NOT FOUND");
}
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
try{
x=xmlhttp.responseXML ;
}
catch(error){
alert("error " + error);
}
alert(x);
alert(xmlhttp.getResponseHeader("Content-Type"));
}
}
xmlhttp.open("POST","ajax/loadContact.php",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("contactID="+contactID);
}
}
我目前得到的是:
x = null
xmlhttp.getResponseHeader("Content-Type") = application/xml.
xmlhttp.responseText //returns the correct page with the correct data.
xmlhttp.open("POST","test.xml",true) //DOES set the responseXML as x = [object Document]
所以我有点迷路了。它被重新定义为application/xml文件,但XMLHttpRequest只设置responseText,而不设置responseXML。
有人能解释一下我没有看到什么吗。事先谢谢!
问题出现在我的PHP文件中:
echo "
<?xml version='"1.0'" encoding='"utf-8'" ?>
不起作用,因为XML声明(?)应该在第一行。
echo "<?xml version='"1.0'" encoding='"utf-8'" ?>
确实有效。
相关文章:
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- php函数的ajax html$_POST值返回null
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null
- Firebase:查询返回null
- 在浅层渲染的反应组件上,Enzyme为道具返回null
- jQuery Multiselect插件返回null长度(应为0?)
- 如何处理getElementById返回Null
- JavaScript中的RegExp返回null
- Javascript 中的 RegEx 返回 Null
- 加载时的getelementbyID返回null
- 谷歌脚本在第二次调用Gmail后返回null
- Indexeddb OpenCursor在特定键值上返回null
- Ajax响应返回null
- 有没有一种方法可以判断图像src是否在javascript内部返回null
- 当JS在Chrome控制台上正常工作时,Selenium JavaScript执行器返回null
- Auth$getUser返回null,即使用户已登录(AngularFire,Firebase)
- Mongoose JS findOne总是返回null
- 为什么我的ajax调用返回null
- 当与全局标志一起使用时,Javascript Regex在后续调用中返回null
- Regex以确定该值在JS中是否有效,返回null