function中ajax,Ajax function

function json2str(data) {

// 0.添加随机因子

data.t = Math.random();

// 1.定义一个数组

var arr = [];

// 2.遍历传入的json对象

for(var key in data){

// 3.将json转化为数组

arr.push(key+"="+encodeURI(data[key]));

}

// 4.将数组转化为固定格式的字符串

// console.log(arr.join("&"));

return arr.join("&");

}

/*

type, url, data, timeout, success, error

var option = {

"type":"get",

"data": {

"name":"nj",

"age":"888"

},

"url":"01-Ajax-get.php",

"error": function(){},

"success":function(){}

}

option.data

*/

function ajax(options) {

// 0.安全处理

if(!options.url){

return;

}

options.type = options.type || "get";

options.data = options.data || null;

options.timeout = options.timeout || 0;

// 1.创建异步对象

if(window.XMLHttpRequest){

var xhr = new XMLHttpRequest();

}else{

var xhr = new ActiveXObject("Microsoft.XMLHTTP");

}

// 对参数进行处理

var str = json2str(options.data);

// 判断请求的类型

if(options.type == "get"){

// 2.设置URL

xhr.open(options.type, options.url+"?"+str, true);

// 3.发送请求

xhr.send();

}else{

// 2.设置URL

xhr.open(options.type, options.url, true);

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

// 3.发送请求

xhr.send(str);

}

// 4.监听请求

xhr.onreadystatechange = function () {

// 5.对请求返回的数据进行处理

if(xhr.readyState == 4){

// 如果已经请求成功, 那么需要清空定时器

clearTimeout(timer);

if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){

// console.log("接收数据成功");

options.success(xhr.responseText);

}else{

// console.log("接收数据失败");

options.error(xhr.status);

}

}

}

// 6.对超时进行处理

if(options.timeout){

var timer = setTimeout(function () {

alert("请求超时");

// 如果请求超时了, 那么应该中断请求

xhr.abort();

}, options.timeout);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值