我正在做一个Ajax的jQuery函数如下:遗漏的类型错误:无法读取空的特性“documentElement”,AJAX
$('.change').live('click',function(){
$('html, body').animate({
scrollTop: $("h4").offset().top
}, 2000);
$('h4').text("Edit Your Video Here :");
$('.drop').text("Drop Your Updated Video Here...");
$('.btn').text("Update");
var videoID = this.id;
xhr1 = new XMLHttpRequest();
//initiate request
xhr1.open('post','beforeUpdate.php',true);//true for asynchronous
xhr1.setRequestHeader('Content-Type',"multipart/form-data");
xhr1.setRequestHeader('vidID',videoID);
xhr1.onreadystatechange = handleResponse;
xhr1.send(null);
});
function handleResponse()
{
if(xhr1.readyState==4)//processing done
{
if(xhr1.status == 200)//response okie
{
var xmlResponse = xhr1.responseXML;
root= xmlResponse.documentElement;
var id = root.getElementsByTagName("ID");
var name = root.getElementsByTagName("Nama");
console.log("ID From Server : "+id);
console.log("Name From Server : "+name);
}
}
}
所以被点击我的按钮{类=变化}时,它会要发送ID来beforeUpdate.php,并在此PHP文件我生成将被送回xhr1.onreadystatechange = handleResponse;
这是我beforeUpdate.php文件的XML文件:
<?php
require 'databaseConnection.php';
openConnection();
$headers = apache_request_headers();
$ID = $headers['vidID'];
$hasil = mysql_query("SELECT * FROM video_management where id=$ID");
$result = mysql_fetch_array($hasil);
//$test = "ID = ".$ID."\r\n"."Name = ".$result['Nama'];
//fwrite($myfile, $test);
//fclose($myfile);
//Generating XML FILE
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
echo '<ID>';
echo $ID;
echo '</ID>';
echo '<Name>';
echo $result['Nama'];
echo '</Name>';
echo '<Type>';
echo $result['Type'];
echo '</Type>';
echo '<Path>';
echo $result['videoPath'];
echo '</Path>';
echo '</response>';
closeConnection();
?>
没有什么错误的,当从数据库中获取数据,我可以获取所有数据并将它们放到相关的xml元素中。
然后问题来自root= xmlResponse.documentElement;
据说Uncaught TypeError: Cannot read property 'documentElement' of null,
所以我在这里做了什么错了......? 非常感谢你......! :)
注: 我的浏览器是Chrome的
xhr1.responseText给你什么? – 2016-07-26 14:53:37
Oooowh ... yeaat ..我从responseText找到了一些东西...给我一下Squint先生... –
其实responseText包含了我的xml响应......这里发生了什么......? :v –