其他分享
首页 > 其他分享> > 【web】Ajax Study Note

【web】Ajax Study Note

作者:互联网

1.Create a XMLHttpRequest Object

(1)For IE7+、Firefox、Chrome、Safari and Opera

variable = new XMLHttpRequest();

(2)For old version like Internet Explorer (IE5 and IE6)

variable = new ActiveXObject("Microsoft.XMLHTTP");
[Example]
var xmlhttp;
if(window.XMLHttpRequest)
{
	xmlhttp  = new XMLHttpRequest;
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

2.Sent Server Request

if you want sent request to service , you'd better use XMLHttpRequest Object's open() and send()

[Example]

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
方法 描述
open( method ,url , async ) 规定请求的类型、URL 以及是否异步处理请求。
* method :请求的类型;GET 或 POST
* url :文件在服务器上的位置
* async :true(异步)或 false(同步)
send( string ) 将请求发送到服务器。
* string :仅用于 POST 请求

image.png

GET

[Example]

xmlhttp.open("GET","/try/ajax/demo_get.php",true);
xmlhttp.send();

In the above example, you might get cached results. To avoid this, Please add a unique ID to the URL.

like this:

xmlhttp.open("GET","/try/ajax/demo_get.php?t=" + Math.random(),true);
xmlhttp.send();

if you want to use "get" to sent message ,please add message to URL.

xmlhttp.open("GET","/try/ajax/demo_get2.php?fname=Henry&lname=Ford",true);
xmlhttp.send();

POST

a easy post request example:

xmlhttp.open("POST","/try/ajax/demo.php",true);
xmlhttp.send();

If you want to sent POST data like HTML form,Please use setRequestHeader() to add HTTP head .and then in "sent()" specify the data you want to send.

xmlhttp.open("POST","try/ajax/demo.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded);
xmlhttp.send("fname=mllt9920&lname=xrilang");
方法 描述
setRequestHeader( header,value ) 向请求添加 HTTP 头。
* header : 规定头的名称
* value : 规定头的值

URL -Files on server

The URL parameter of the open () method is the address of the file on the server.

xmlhttp.open("GET"."ajax_test.html",true);

Asynchronous JavaScript and XML - True or False

!if XMLHttpRequest Object want to use in AJAX, The async parameter of open() method must set true.

xmlhttp.open("GET","ajax_test.html",true);
Async=true

When you use async=true , Specify the function to execute when the response is in the ready state in the onreadystatechange event:

xmlhttp.onreadystatechange = function()
{
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200 )
	{
		document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
	}
}

readyState

0:初始化,XMLHttpRequest对象还没有完成初始化
1:载入,XMLHttpRequest对象开始发送请求
2:载入完成,XMLHttpRequest对象的请求发送完成
3:解析,XMLHttpRequest对象开始读取服务器的响应
4:完成,XMLHttpRequest对象读取服务器响应结束
————————————————
版权声明:本文为CSDN博主「LIU SHIHUA」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/S_imon_/article/details/104722438

State

1xx:信息响应类,表示接收到请求并且继续处理
2xx:处理成功响应类,表示动作被成功接收、理解和接受
3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理
4xx:客户端错误,客户请求包含语法错误或者是不能正确执行
5xx:服务端错误,服务器不能正确执行一个正确的请求

100——客户必须继续发出请求
101——客户要求服务器根据请求转换HTTP协议版本
200——交易成功
201——提示知道新文件的URL
202——接受和处理、但处理未完成
203——返回信息不确定或不完整
204——请求收到,但返回信息为空
205——服务器完成了请求,用户代理必须复位当前已经浏览过的文件
206——服务器已经完成了部分用户的GET请求
300——请求的资源可在多处得到
301——删除请求数据
302——在其他地址发现了请求数据
303——建议客户访问其他URL或访问方式
304——客户端已经执行了GET,但文件未变化
305——请求的资源必须从服务器指定的地址得到
306——前一版本HTTP中使用的代码,现行版本中不再使用
307——申明请求的资源临时性删除
400——错误请求,如语法错误
401——请求授权失败
402——保留有效ChargeTo头响应
403——请求不允许
404——没有发现文件、查询或URl
405——用户在Request-Line字段定义的方法不允许
406——根据用户发送的Accept拖,请求资源不可访问
407——类似401,用户必须首先在代理服务器上得到授权
408——客户端没有在用户指定的饿时间内完成请求
409——对当前资源状态,请求不能完成
410——服务器上不再有此资源且无进一步的参考地址
411——服务器拒绝用户定义的Content-Length属性请求
412——一个或多个请求头字段在当前请求中错误
413——请求的资源大于服务器允许的大小
414——请求的资源URL长于服务器允许的长度
415——请求资源不支持请求项目格式
416——请求中包含Range请求头字段,在当前请求资源范围内没有range指示值,请求也不包含If-Range请求头字段
417——服务器不满足请求Expect头字段指定的期望值,如果是代理服务器,可能是下一级服务器不能满足请求
500——服务器产生内部错误
501——服务器不支持请求的函数
502——服务器暂时不可用,有时是为了防止发生系统过载
503——服务器过载或暂停维修
504——关口过载,服务器使用另一个关口或服务来响应用户,等待时间设定值较长
505——服务器不支持或拒绝支请求头中指定的HTTP版本
Async=false

To use async = false, change the third parameter in the open () method to false:

xmlhttp.open("GET","test1.txt",false);

! When you use async = false, do not write the onreadystatechange function - just put the code after the send () statement

xmlhttp.open("GET","/try/ajax/ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

Get Server Response

属性 描述
responseText 获得字符串形式的响应数据。
responseXML 获得 XML 形式的响应数据。

responseText

If the response which from server is not XML ,please use responseText.

responseText return String . you can use it like this:

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

responseXML

if the response which from server is XML , and need analysis as XML Object , please use responseXML.

xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
    txt=txt + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("myDiv").innerHTML=txt;

标签:web,xmlhttp,请求,GET,true,Note,Ajax,服务器,open
来源: https://www.cnblogs.com/mllt/p/ajax20220310.html