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. html局部打印

    html页面局部打印的小栗子 只要修改点击打印的按钮和打印的div区域的id就行啦 <!DOCTYPE html> <html> <head> <title& ...

  2. php : MVC 演示(使用单例工厂)

    此例子是MVC的简单应用, 要达到的效果如下: 用户列表: 姓名 年龄 学历 兴趣 出生地 账号创建时间 操作 keen 20 高中 篮球,足球 广东 2016-11-08 10:00:31 删除 a ...

  3. spring 注解重复(防重复请求)

    1.配置拦截器 spring-mvc.xml <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/ ...

  4. 个人博客作业Week3

    一.调研 下载并使用,按照描述的bug定义,找出几个功能性的比较严重的bug.至少两个.用专业的语言描述(每个bug 不少于 40字),如有必要,可以配图. 电脑用户未登录就能使用单词本功能,万一是用 ...

  5. 深圳楼市2007vs2016

    昨日新政,虽在预料之中,但心中未免有些感慨.今日在回深的火车上,突然由此让我回想起了2007年的那场深圳房价大溃败.忍不住写上一段供大家参考.前奏:2000年后的深圳楼市在2004年之前可以说是波澜不 ...

  6. 疑惑的 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L

    在MAVEN项目里面,在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transa ...

  7. 高并发下MySQL出现checking permissions

    在某些数据访问层框架中,会使用show full tables from test like 'demo',来检查数据库的状态.当数据库中表的数量较少时,并没有出现严重的问题.但是当数据库中的表数量多 ...

  8. ADO.NET 使用通用数据库操作类Database (SQL Server)

    一.Web.config配置 <connectionStrings> <add name="constr_name" connectionString=" ...

  9. css学习记录

    1 !important 表示此属性需要优先考虑: <head>    <title>Page Title</title>    <style type=&q ...

  10. url的内容及格式

    url的内容及结构: url格式: