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. Windows安装Scrapy遇坑解决办

    PS: Windows真心不适合开发.且行且珍惜.... 坑: error: Setup script exited with error: Microsoft Visual C++ 9.0 is r ...

  2. jquery总结06-动画事件01-基础显示和隐藏

    动画事件 .hide(option) 动画隐藏  会保存元素的原始属性值 $("#a2").hide({ duration: 3000, complete: function() ...

  3. hdu-5525 Product(费马小定理)

    题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...

  4. HTML meta锚点跳转 小tips

    小tips meta锚点跳转 http://www.zhangxinxu.com/wordpress/2015/03/meta-http-equiv-refresh-content/

  5. Http referer origin

    为了防止CSRF的攻击,我们建议修改浏览器在发送POST请求的时候加上一个Origin字段,这个Origin字段主要是用来标识出最初请求是从哪里发起的.如果浏览器不能确定源在哪里,那么在发送的请求里面 ...

  6. CDN加速原理

    原文链接:http://www.72e.net/cdnspeed/cdnyuanli.aspx 首先,让我们来看一下传统的Internet网络的基本结构和数据传输情况,如下图所示. Internet  ...

  7. spa 单页面解决浏览器back front 问题

    1.angular router reloadOnSearch:true(default) | false  默认为true,当url的hash发生改变,页面新渲染,component会重新加载(初始 ...

  8. Solr Cloud - SolrCloud

    关于 Solr Cloud Zookeeper 入门,介绍 原理 原封不动转自 http://wiki.apache.org/solr/SolrCloud/ ,文章的内存有些过时,但是了解原理. Th ...

  9. java打印Jni层log

    在eclipse上新建jni工程可以参考:http://www.cnblogs.com/ashitaka/p/5953708.html 要在java层打印c的log必须引入这个头文件的宏定义: #if ...

  10. join / left join / right join

    1 可以自己join自己 SELECT * FROM table_1 t1join table_1 ton t.target_id = t1.target_id 2 join要双方都满足才可以出现结果 ...