http://www.cnblogs.com/rubylouvre/archive/2013/01/08/2851051.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> Ajax = function(){ function request(url,opt){ function fn(){} var async = opt.async !== false, method = opt.method || ‘GET‘, data = opt.data || null, success = opt.success || fn, failure = opt.failure || fn; method = method.toUpperCase(); if(method == ‘GET‘ && data){ url += (url.indexOf(‘?‘) == -1 ? ‘?‘ : ‘&‘) + data; data = null; } var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject(‘Microsoft.XMLHTTP‘); xhr.onreadystatechange = function(){ _onStateChange(xhr,success,failure); }; xhr.open(method,url,async); if(method == ‘POST‘){ xhr.setRequestHeader(‘Content-type‘, ‘application/x-www-form-urlencoded;‘); } xhr.send(data); return xhr; } function _onStateChange(xhr,success,failure){ if(xhr.readyState == 4){ var s = xhr.status; if(s>= 200 && s < 300){ success(xhr); }else{ failure(xhr); } }else{} } return {request:request}; }(); console.log(Ajax.request("http://baidu.com",{method:"POST"})); </script> </body> </html>
时间: 2024-12-11 12:39:05