博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Primitive JS completion of AJAX
阅读量:4448 次
发布时间:2019-06-07

本文共 3710 字,大约阅读时间需要 12 分钟。

Firstly , let us explain XMLHttpRequest open(), send(), readyState

1. open(method, url, async, user, password) : create request, initialize parameters for send()
method: 'POST' , 'GET' , 'HEAD' or 'post' , 'get' , 'head', case insensitive
if set method to 'POST', the size of data sended to server is limited to 4MB
if set method to 'GET', the size is limited to 256KB 
url: the request address, browser has the same origin security policy, so the script should has the same hostname and portname with the url
async: 'true' or 'false', 'true' means the request is asynchronous, default to true, 'false' means synchronous
user,
password: optional, which set the authentication of access url, if it is setted, it will override the default authentication owned by url 

 

2. send(body) : send request to server
if the method parameter in open() is set to 'POST' (which in from, sety <form  method='post'>)
We need set header for the request: xmlHttpReq.setRequestHeader('content-Type', 'application/x-www-form-urlencoded');
The request data can only be sent by send()
In server side, using Request.Form() to get the request form data
if the method parameter in open() is set to 'GET'
No need to set header for the request
The request data can be contained by url to be sent to server, or sent by send()
In server side, using Request.QueryString() to get the url address parameter or the request form data

 

3. readyState: the state of the request
The five possible values are 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, and 4 = complete
0(
uninitialized): create XMLHttpRequest object, when successfully, the readyState = 0
1(
loading): call open(url, method, sync), according to the parameters, initialize the XMLHttpRequest object; 
call send(), send request to server. readyState = 1 means the request is sending...
2(
loaded):  send() completed, receive entire response, which is raw data that could not used directly. readyState = 2 means received entire response.
3(
interactive): parsing the response, according to MIME Type contained by header in server response, 
parsing data to form of responseBody, responseText or responseXML. readyState = 3 means the response is parsing...
4(
complete): The parse process is completed. Access data by XMLHttpRequest object attribute. readyState = 4 means the parse is done.

 

Then List some examples:

Exp1: Asnynchronous Style
var xmlHttpReq;
function startAjax() {
xmlHttpReq = window.ActiveXObject ? window.ActiveXObject : window.XMLHttpRequest;
if(!xmlHttpReq) {
alert("Create failed!")
}
var body = 'name=brittany&age=24';
xmlHttpReq.open('POST', 'test.php', true);
xmlHttpReq.onreadystatechange = function() {
if(xmlHttpReq.readyState == 4) {
if(xmlHttpReq.status == 200) {
//...
}
}
}
xmlHttpReq.setRequestHeader('Content-Type', 'applicaiton/x-www-form-urlencoded');
xmlHttpReq.send(body);
}
Exp2: Synchronous Style
var xmlHttpReq;
function startAjax() {
//...
var body = 'name=brittany&age=24';
xmlHttpReq.open('POST', 'test.php', false);
xmlHttpReq.setRequestHeader('Content-Type', 'applicaiton/x-www-form-urlencoded');
xmlHttpReq.send(body);
}
Exp3: Asnynchronous Get Style
var xmlHttpReq;
function startAjax() {
//...
var url = 'test.php' + '?name=' + 'brittany' + '?age=' + '24';
xmlHttpReq.open('GET', url, true);
xmlHttpReq.onreadystatechange = function() {
if(xmlHttpReq.readyState == 4) {
if(xmlHttpReq.status == 200) {
//...
}
}
}
xmlHttpReq.send(null);
}
or 
var xmlHttpReq;
function startAjax() {
//...
var body = 'name=brittany&age=24';
xmlHttpReq.open('GET', 'test.php', false);
xmlHttpReq.setRequestHeader('Content-Type', 'applicaiton/x-www-form-urlencoded');
xmlHttpReq.send(body);
}

转载于:https://www.cnblogs.com/sun-mile-rain/p/4337339.html

你可能感兴趣的文章
评估软件上线标准
查看>>
敏捷开发流程
查看>>
APP兼容性测试(三)测试方案设计
查看>>
React的性能优化 - 代码拆分之lazy的使用方法
查看>>
React的新特性 ---- Hooks ---- 的基本使用
查看>>
History Introduction to Mining Industry of Czech
查看>>
富文本框
查看>>
mysql 恢复备份
查看>>
LeetCode-Create Maximum Number
查看>>
Process Class (System.Diagnostics)
查看>>
Core Animation系列之CADisplayLink
查看>>
dedecms标签调用大全
查看>>
《与小卡特一起学Python》Code1
查看>>
[ZJOI2007]捉迷藏 (点分树+堆*3)
查看>>
leetcode 412. Fizz Buzz
查看>>
对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑
查看>>
关于日历的算法
查看>>
[QT编程]QT实现的一个渐隐渐显窗体
查看>>
在Web工程中引入Jquery插件报错解决方案
查看>>
大学总结之影响我最深的十本书
查看>>