博客
关于我
封装一个简易版的ajax操作对象
阅读量:432 次
发布时间:2019-03-06

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

/** * 发送ajax请求 * @type {Object} * 使用方法如下: * $ajax.request( *     method: "post",            //请求方式 *     url:    "index.php",       //请求的服务器地址 *     data:   "name=itbsl&age=20",//请求时携带的参数 *     dataType: "xml",           //期望服务器回应的数据 *     callback: function(res) {  //请求成功后的回调函数 *         //处理数据 *     } * ); */var $ajax = {    request: function(obj) {        //创建对象        var xmlhttp;        if(window.XMLHttpRequest) {            //code for IE7+, Firefox, Chrome, Opera, Safari            xmlhttp = new XMLHttpRequest();        } else {            //code for IE6, IE5            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        }        //建立连接        if(obj.method == 'get') {            xmlhttp.open(obj.method, obj.url+"?"+obj.data+"&"+Math.random(), true);            xmlhttp.send();        } if(obj.method == 'post') {            xmlhttp.open(obj.method, obj.url, true);            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");            xmlhttp.send(obj.data);        }        //监视请求的状态        xmlhttp.onreadystatechange = function () {            if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {                if(obj.dataType == 'xml') {                    obj.callback(xmlhttp.responseXML);                } else if(obj.dataType == 'text') {                    eval("var res = " + xmlhttp.responseText);                    obj.callback(res);                }            }        }    }}

转载地址:http://ybwyz.baihongyu.com/

你可能感兴趣的文章
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理一
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
netty的HelloWorld演示
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty的网络框架差点让我一夜秃头,哭了
查看>>
Netty相关
查看>>
Netty简介
查看>>
Netty线程模型理解
查看>>
netty解决tcp粘包和拆包问题
查看>>
Netty速成:基础+入门+中级+高级+源码架构+行业应用
查看>>
Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
查看>>
netty(1):NIO 基础之三大组件和ByteBuffer
查看>>
Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
查看>>
Netty:原理架构解析
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>