js封装ajax

    xiaoxiao2022-07-13  150

    function ajax(method, url, callback, data, flag) { var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); }else { xhr = new ActiveXObject('Microsoft.XMLHttp') } xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status == 200) { callback(xhr.responseText); }else { console.log('error'); } } } method = method.toUpperCase(); if(method == 'GET') { var date = new Date(), timer = date.getTime(); xhr.open(method, url + '?' + data + '&timer=' + timer, flag); xhr.send(); }else if(method == 'POST') { xhr.open(method, url, flag); xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xhr.send(data); } }
    最新回复(0)