Ajax入坑笔记

XMLHTTPRequest

open(method,url,async)

method:规定请求的类型;url:文件在服务器上的位置;async:true(异步)或false(同步)

setRequestHeader(header,value)

herder:规定头的名称;value:规定头的值将请求发送到服务器

onreadystatechange

该属性用来指定xhr对象状态改变时的时间函数

当readyState改变时,调用一次该函数

send(string)

string:仅用于POST请求

xmlhttp.readyState

0:xhr对象还没完成初始化;

1:xhr对象开始发送请求;

2:xhr对象的请求发送完成;

3:xhr对象开始读取服务器的相应;

4:xhr对象读取服务器响应结束。

xmlhttp.status

状态码 F12抓包可以查看具体的

xmlhttp.respenseText

xmlhttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);

返回的信息

PS:

var xmlhttp;
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 浏览器执行代码
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}

一个网页有多个ajax请求时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var xmlhttp;
function loadXMLDoc(url,cfunc) //该网页所有ajax都通过该函数完成
{
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("/try/ajax/ajax_info.txt",function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
});
}

eg:https://www.runoob.com/php/php-ajax-database.html

0%