原生 Ajax 请求案例/模板
发布时间:2020-12-16 01:46:41 所属栏目:百科 来源:网络整理
导读:Ajax 原生 创建 Ajax 异步请求对象 var http; if (window.XMLHttpRequest) { // code for IE7+,Firefox,Chrome,Opera,Safari http = new XMLHttpRequest();} else { // code for IE6,IE5 http = new ActiveXObject( "Microsoft.XMLHTTP" );} 注册 response
Ajax 原生创建 Ajax 异步请求对象var http;
if (window.XMLHttpRequest) { // code for IE7+,Firefox,Chrome,Opera,Safari
http = new XMLHttpRequest();
} else { // code for IE6,IE5
http = new ActiveXObject("Microsoft.XMLHTTP");
}
注册 response 处理方法注册处理 http.onreadystatechange=function() {
if (http.readyState==4 && http.status==200) {
alert(http.responseText);
}
};
发送请求发送 GET 请求http.open("GET","test1.txt",true);
http.send();
发送 POST 请求http.open("POST","ajax_test.asp",true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http.send("fname=Bill&lname=Gates");
属性说明方法:open() http.open(METHOD,URL,ASYNC); 方法:send() http.send(BODY); 属性:readyState http.readyState => (Integer) 属性:onreadystatechange http.onreadystatechange => (callback) 属性:status http.status => (Integer) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |