标签:调用 字符 数字 方式 状态 obj send 刷新 cat
一、主体:封装ajax函数:ajaxFunc
function ajaxFunc(method, url, data, callback, flag) { //(1)创建ajax对象 var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { xhr = new ActiveXObject(‘Microsoft.XMLHttp‘); } //(2)请求方式get||post method = method.toUpperCase(); if(method == ‘GET‘){ xhr.open(method, url, flag); xhr.send(); }else if(method == ‘POST‘){ xhr.open(method, url, flag); xhr.setRequestHeader(‘Content-type‘, ‘application/x-www-form-urlencoded‘); xhr.send(data); } //(3)监听请求数据 xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ callback(xhr.responseText); } xhr = null; } } } //(4)回调函数对数据处理 function callback(requestData){ var obj = JSON.parse(requestData); }
标签:调用 字符 数字 方式 状态 obj send 刷新 cat
原文地址:https://www.cnblogs.com/zhuzixi/p/9109336.html