documentElement为null - 错误 [英] documentElement is null - error

查看:95
本文介绍了documentElement为null - 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎!这是我的代码,我是ajax的新手,我只是按照一些教程,但当我完成代码时,这个错误来自开发人员工具



未捕获的TypeError :无法在第45行读取属性'DocumentElement'的null



i不能得到这个,有人能够解决我的问题,我已经在3-上问了这个问题4个地方,但没有人回答,请帮助

 var xmlHttp = createXmlHttpRequestObject(); // xmlHttp  - > Ajax中的主要对象

函数createXmlHttpRequestObject(){
var xmlHttp;

if(window.ActiveXObject){//当用户不使用IE
试试{
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
} catch(e){
xmlHttp = false;
}
}
else {
try {
xmlHttp = new XMLHttpRequest();
} catch(e){
xmlHttp = false;
}
}

if(!xmlHttp){
alert(Errorr yrrrrrr !!);
}
else {
return xmlHttp; //因为在第一行,它等于函数所以需要返回
}
}

函数process(){//接受那个Communication对象(xmlHttp)并将请求发送到服务器
if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4){
name = encodeURIComponent(document.getElementById('mname')。value);
xmlHttp.open(GET,muslims.php?name =+ name,true); //如果应该处理asyncrounsly
xmlHttp.onreadystatechange = handleServerResponse; //这会在您向服务器发送请求后处理服务器响应
xmlHttp.send(null);
}
else {
setTimeout('process()',1000);
}
}

函数handleServerResponse(){//在php文件中发回xml代码
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){// 200表示通信正常
xmlResponse = xmlHttp.responseXML; //基本上xmlResponse现在是来自php文件的xml
xmlDocumentElement = xmlResponse.DocumentElement;
message = xmlDocumentElement.FirstChild.data; //此消息将等于来自php文件的回声之一
document.getElementById('show')。innerHtml ='< span style =color:red>'+ message +'< /跨度>';
setTimeout('process()',1000);
}
else {
alert(yarrrrrrr !!!中的错误);
}
}
}

解决方案

php文件返回的XML可能不正确并导致问题。请查看以下链接



responseXML.documentElement返回null对象 [ ^ ]


不要使用传统的Ajax请求。尝试jquery ajax来完成此任务。 />


JQuery 网站的项目中包含jquery.js库

然后在你用过的页面上引用它

现在进行如下的ajax调用



 


(文件)。就绪(函数(){

hey! this is my code , i am very new to ajax and i was just following some tutorial , but when i completed the code , this error came on developer tools

Uncaught TypeError: Cannot read property 'DocumentElement' of null on line 45

i don't get this, can someone plzz solve my problem , i've asked this question on 3-4 places but noone's answering , please help

var xmlHttp = createXmlHttpRequestObject();          //xmlHttp --> a main object in Ajax 

function createXmlHttpRequestObject(){
	var xmlHttp;
	
	if(window.ActiveXObject){                        	//when user not using IE	
		try{				
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){			
				xmlHttp = false;
			}
    }		
	else{	
		try{				
				xmlHttp = new XMLHttpRequest();
		}catch(e){			
				xmlHttp = false;
		}			
	}
    
	if(!xmlHttp){		
		alert("Errorr yrrrrrr!!");
	}
	else{		
		return xmlHttp;                                 //because in the first line , it is equal to the function so needs to return
	}
}

function process(){							                   //takes that Communication object(xmlHttp) and sends request to the server
	if(xmlHttp.readyState==0 ||  xmlHttp.readyState==4){		
		name = encodeURIComponent(document.getElementById('mname').value);                    
		xmlHttp.open("GET", "muslims.php?name="+name, true);             //if it should be handled asyncrounsly 
		xmlHttp.onreadystatechange = handleServerResponse;      //this handles the server response after you send request to server
		xmlHttp.send(null);																	
	}
	else{		
		setTimeout('process()',1000);
	}
}

function handleServerResponse(){                           //sends back xml code in php file
	if(xmlHttp.readyState==4){		
		if(xmlHttp.status==200){						   //200 means communication went normal			
			xmlResponse = xmlHttp.responseXML;             //basically xmlResponse is now your xml from php file
			xmlDocumentElement = xmlResponse.DocumentElement;
			message = xmlDocumentElement.FirstChild.data;        //this message will be equal to one of the echos from php file
			document.getElementById('show').innerHtml = '<span style="color:red">'+ message + '</span>';
			setTimeout('process()',1000);
		}
		else{	
		alert("Error in the end yarrrrrrr!!!");
		}
	}
    }

解决方案

The XML returned by your php file might not be correct and causing issue. Please check below link

responseXML.documentElement returns null object[^]


Don't go for conventional Ajax request.Try jquery ajax to accomplish this task.

include jquery.js library in your project from JQuery site
Then reference it on your used page
Now make an ajax call as below


(document).ready(function(){


这篇关于documentElement为null - 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆