function ajax(options){ // 1.处理默认参数 var {type,url,success,error,data,timeout} = options; type = type || "get"; data = data || {}; timeout = timeout || 2000; // 2.解析要发送的数据 var str = ""; for(var i in data){ str += `${i}=${data[i]}&`; } // 3....
javascript - 封装原生js实现ajax 1/*2* ajax方法3*/4varAjax =function() {5varthat =this;6//创建异步请求对象方法7that.createXHR =function() {8if(window.XMLHttpRequest) {//IE7+、Firefox、Opera、Chrome 和Safari9returnnewXMLHttpRequest();10}elseif(window.ActiveXObject) {//IE6 及以下11varv...
状态值为4if(xhr.readyState===4){if(xhr.status===200){console.log(xhr.responseText);}else{console.error(xhr.statusText);}}};xhr.onerror=function(e){console.error(xhr.statusText);};xhr.open('GET','/endpoint',true);xhr.send(null);...
代码语言:javascript 代码运行次数:0 运行 AI代码解释 xhr.onreadystatechange=function() { //判断readyState就绪状态是否为4,判断status响应状态码是否为200 if (xhr.readyState==4 && xhr.status==200) { //获取服务器的响应结果 var responseText = xhr.responseText; alert(responseText); } } 状态 描述 rea...
代码语言:javascript 代码运行次数:0 运行 AI代码解释 //创建xhr对象letxhr=newXMLHttpRequest();//调用open函数xhr.open('get','http://www.liulongbin.top:3006/api/getbooks?id=1')//调用send函数xhr.send()//监听事件xhr.onreadystatechange=function(){if(xhr.readyState===4&&xhr.status===200){/...
window[callbackname]=function(data) { callback(data); document.body.removeChild(oScript); } } }else{ alert('请打开服务器运行!'); } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. ...
xhr.onreadyStateChange = function () { // 每次 readyState 改变的时候都会触发该事件 // 我们就在这里判断 readyState 的值是不是到 4 // 并且 http 的状态码是不是 200 ~ 299 if (xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)) { ...
var ajax = function(options){ for(var i in defaultOption){ options[i] = options[i] || defaultOption[i]; } // http 对象 var xhr = new XMLHttpRequest(); var url = options.url; xhr.open(options.type, url); // 监听 xhr.onreadystatechange = function(){ ...
function ajaxGet(url, callback, data) { //1.解析发送的数据 data = data || {}; //修复bug1:参数为空变为空对象 var str = ""; for (var i in data) { str += `${i}=${data[i]}&`; //拼接get的数据格式 } //2.拼接url ...
function request(){ //第一步:创建XMLHttpRequest对象 var xmlHttp; if (window.XMLHttpRequest) { //非IE xmlHttp = new XMLHttpRequest(); console.log('创建了XMLHttpRequest对象'); } else if (window.ActiveXObject) { //IE xmlHttp = new ActiveXObject("Microsoft...