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);
}

Primitive JS completion of AJAX的更多相关文章

  1. js 封装原生ajax

    jquery框架的ajax方法固然好用,但是假如某天我们的项目不能引入jquery或项目需求很简单,没有很多交互功能,只需要ajax,这时引入jquery库会造成资源浪费,也会显得页面臃肿.这时我们就 ...

  2. 引入js文件,ajax不执行操作

    今天写了一个页面,在页面中写的可以执行,但是放到js里面,引入到页面,ajax却不执行了,仔细一看原来是路径的原因 ${pageContext.request.contextPath} 为获取项目名称 ...

  3. ThinkPHP 中使用 IS_AJAX 判断原生 JS 中的 Ajax 出现问题

    问题: 在 ThinkPHP 中使用原生 js 发起 Ajax 请求的时候.在控制器无法使用 IS_AJAX 进行判断.而使用 jQuery 中的 ajax 是没有问题的. 在ThinkPHP中.有一 ...

  4. JS实现的ajax和同源策略

    一.回顾jQuery实现的ajax 首先说一下ajax的优缺点 优点: AJAX使用Javascript技术向服务器发送异步请求: AJAX无须刷新整个页面: 因为服务器响应内容不再是整个页面,而是页 ...

  5. 使用spin.js优化等待ajax返回时的页面效果

    [本文出自天外归云的博客园] 最近在做一个JIRA信息统计的系统,在统计JIRA关联信息的过程中由于需要等待ajax返回结果到前端,时间较长,所以要添加一段等待时的loading画面,使用spin.j ...

  6. js 实现对ajax请求面向对象的封装

             AJAX 是一种用于创建高速动态网页的技术.通过在后台与server进行少量数据交换.AJAX 能够使网页实现异步更新.这意味着能够在不又一次载入整个网页的情况下,对网页的某部分进行 ...

  7. js进阶 14-9 ajax事件有哪些

    js进阶 14-9 ajax事件有哪些 一.总结 一句话总结:ajax开始时事件.发送时事件,请求完成时事件,请求成功时事件,请求结束时事件,请求错误时事件事件. 1.ajax事件的监听对象是谁? 都 ...

  8. js进阶 14-6 $.ajax()方法如何使用

    js进阶 14-6 $.ajax()方法如何使用 一.总结 一句话总结:$.ajax([settings])settings可选.用于配置Ajax请求的键值对集合. 1.$.ajax()的特点是什么( ...

  9. js进阶课程ajax简介(ajax是浏览器来实现的)

    js进阶课程ajax简介(ajax是浏览器来实现的) 一.总结 1.ajax使用需要服务器支持,比如phpstudy 2.ajax是浏览器支持的功能:ajax有个核心对象XMLHttpRequest, ...

随机推荐

  1. SQL2008 的 日期数据类型

    摘要 你是否曾经想在数据库中存储一个日期而没有时间部分,或者想存储一个时间值希望有更高的精度?在SQL Server 2008的介绍中,微软介绍了一些新的日期数据类允许你只存储一个日期.更高精度的时间 ...

  2. SQL数据库中字段类型 与C#中的对应字段类型

    数据库中的字段类型和对应的C#中的对应字段类型 数据库                 C#程序int int32text stringbigint int64binary System.Byte[] ...

  3. 【C语言训练】尼科彻斯定理

    题目描述验证尼科彻斯定理,即:任何一个正整数的立方都可以写成一串连续奇数的和. 输入任一正整数 输出该数的立方分解为一串连续奇数的和 样例输入13样例输出13*13*13=2197=157+159+1 ...

  4. 调用微信JsAPI端获取位置

    public partial class test : BasePage { protected test() { AccessPage = PageWebType.WX; } protected s ...

  5. Rails中的缓存

    最近学习Rails. 看到如下代码: <% if notice %> <p id="notice"><%= notice %></p> ...

  6. Java分别与MySQL、Oracle、SQL Server数据库建立连接

    1.与MySQL连接 jar包下载地址: Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动(MySQL的jar包) String u ...

  7. point

    7.10 []getch->_getch []错误:fatal error C1189: #error : EasyX is only for C++ :.c改为.cpp

  8. NFC

    NFC手机是指带有NFC模块的手机.带有NFC模块的手机可以做很多相应的应用.NFC是Near Field Communication缩写,即近距离无线通讯技术.在13.56MHz频率运行于20厘米距 ...

  9. 【图像】Matlab图像标定工具箱

    参考教程: Matlab工具箱教程  http://www.vision.caltech.edu/bouguetj/calib_doc/ 摄像机模型  http://oliver.zheng.blog ...

  10. maven-web项目中的一些小问题

    1.最新的jetty容器 org.eclipse.jetty 需要JDK1.8的支持. 2.在容器中发布WEB项目时web 中的pom.xml的依赖关系会丧失,依赖和插件需要单独完全编写(尽管IDE会 ...