xmlhttprequest用法
<!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">
<script language='javascript' type='text/javascript'>
1 var req; var xmlHttpRequest; // 定义变量,用来创建xmlhttprequest对象,用于存放XMLHttpRequest2 createXMLHttpRequest(); // 调用创建对象的方法
3 xmlhttp.onreadystatchange = callback; 注册回调函数;onreadystatchagne 每个状态改变时都会触发事件处理器,通常会调用javascript函数
4 xmlhttp.open('GET','AJAX?name='userName,true); //open方法几个重要的参数:get/post,服务器地址,
//XMLhttpRequest对象的交互方式即同步/异步,true表示异步方式) 使用open方法设置和服务器交互的基本信息
5 xmlhttp.send(null); //设置发送的数据,开始和服务器进行交互
- if(xmlhttp.readyState == 4){
- //表示和服务器端的交互已经完成
- if(xmlhttp.status == 200){
- //表示服务器的响应代码是200,正确的返回了数据
- //纯文本数据的接受方法
- var message = xmlhttp.responsText();
- //如果使用的是DOM对象的接受方法,则
- //var doxXml = xmlhttp.responseXML();
- //但是有一个前提,服务器端需要设置content-type为text/xml
- var div = document.getElementById("页面div的ID")
- div.innerHTML = message;
- }
- }
- }
在回调函数中判断交互是否结束,响应是否正确,并根据需要获取服务器端返回的数据,更新页面内容
问题1.unicorn.xml取不到值.
问题2.是不是接口没连接上
<script type='text/javascript'>
var client = new XMLHttpRequest();
function createXMLHttpRequest(){
xmlHttp.onreadystate = handlestate
};
function handler (){
xmlHTTP = new xmlhttprequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml",true);
client.send(unicorn.xml);
document.write (unicorn.xml)
}
</script>