Primitive JS completion of AJAX

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

}

时间: 2024-08-10 04:16:53

Primitive JS completion of AJAX的相关文章

Day67:JS实现的ajax

JS实现的ajax AJAX核心(XMLHttpRequest) 其实AJAX就是在Javascript中多添加了一个对象:XMLHttpRequest对象.所有的异步交互都是使用XMLHttpServlet对象完成的.也就是说,我们只需要学习一个Javascript的新对象即可. ? 1 var xmlHttp = new XMLHttpRequest():(大多数浏览器都支持DOM2规范) 注意,各个浏览器对XMLHttpRequest的支持也是不同的!为了处理浏览器兼容问题,给出下面方法来

基于JS的DOM 编程基础和Json语法及JS下的AJAX基础

1.在dom编程中:     注意:使用dom操作一般在页面完全载入之后,一般在window_onload事件里操作dom nodeName  表示节点的名称如:<input   type= "button"  value="确定" />    此时nodeName=“input” ; nodeValue  表示节点的值如<p>aaa</p> 此时的nodeValue="aaa",而nodeName="

mockjax MOCK.js的拦截ajax请求

今天看了下 mock.js的拦截请求 .https://github.com/nuysoft/Mock/blob/master/src/mockjax.js //覆盖(拦截) Ajax 请求,目前内置支持 jQuery.Zepto.KISSY 拦截请求的前提是基于各类库的 内置方法来进行拦截. 比如jquery. 在$.ajax() 之前 会有一个前置过滤器$.ajaxPrefilter() 先进行处理,然后根据参数的不同来分发请求(模拟或者直接发送请求). 在模拟的时候 可以根据给出的参数 返

js方法内Ajax请求数据判断,验证无效(OnClientClick=&quot;return Method();&quot;),还是直接执行后台代码

function CheckAdd() { var flag = true; $.ajax({ cache: false, async: false, url: "/ajaxpage/getajax.aspx?t=adjserviceclientlist&Cellphone=" + Cellphone + "&a=" + Math.random(), dataType: 'json', success: function (data) { if (d

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

AJAX 是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 在js中使用ajax请求一般包含三个步骤: 1.创建XMLHttp对象 2.发送请求:包括打开链接.发送请求 3.处理响应 在不使用任何的js框架的情况下,要想使用ajax,可能需要向下面一样进行代码的编写 var xmlHttp = xmlHttpCreate();//创建对象 xmlHttp.onreadysta

JS实现的ajax和同源策略

一.回顾jQuery实现的ajax 首先说一下ajax的优缺点 优点: AJAX使用Javascript技术向服务器发送异步请求: AJAX无须刷新整个页面: 因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高: jquery 实现的ajax 1 <!DOCTYPE html> 2 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <

Django 【第十九篇】JS实现的ajax、同源策略和前端jsonp解决跨域问题

一.回顾jQuery实现的ajax 首先说一下ajax的优缺点 优点: AJAX使用Javascript技术向服务器发送异步请求: AJAX无须刷新整个页面: 因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高: jquery 实现的ajax index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <t

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

问题: 在 ThinkPHP 中使用原生 js 发起 Ajax 请求的时候.在控制器无法使用 IS_AJAX 进行判断.而使用 jQuery 中的 ajax 是没有问题的. 在ThinkPHP中.有一个判断是 ajax 请求的常量 IS_AJAX: Ajax 请求常用的有两种情况:一种是原生 js 的 ajax 请求.一种是 jQuery 的 ajax 请求. 分析: 先看看使用 jQuery 中使用 ajax 发送请求的时候的头信息: Accept: application/json, tex

原生JS写的ajax函数

参照JQuery中的ajax功能,用原生JS写了一个ajax,功能相对JQuery要少很多,不过基本功能都有,包括JSONP. 调用的方式分为两种: 1. ajax(url, {}); 2. ajax({}); 调用的方法参照JQuery的ajax,只是 不需要写$.ajax ,只需要写 ajax 就可以了. 代码如下: !function () { var jsonp_idx = 1; return ajax = function (url, options) { if (typeof url