var http_request = false;function send_request(method,url,content,responseType,callback){ http_request = false; if(window.XMLHttpRequest) { //现代浏览器 http_request = new XMLHttpRequest(); if(http_request.overrideMimeType) { http_request.overrideMimeType("text/xml"); } } else if(window.ActiveXObject) { //老IE浏览器 try{ http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} } } if(!http_request) { window.alert("Can‘t create XMLHttpRequest"); return false; } if(responseType.toLowerCase() == "text") { //http_request.onreadystatechange = processTextResponse; http_request.onreadystatechange = callback; } else if(responseType.toLowerCase == "xml") { //http_request.onreadystatechange = processXMLResponse; http_request.onreadystatechange = callback; } else { window.alert("error"); return false; } if(method.toLowerCase() == "get") { http_request.open(method, url, true); } else if(method.toLowerCase() == "post") { http_request.open(method, url, true); http_request.setRequestHeader("content-Type","application/x-www-form-urlencoded"); } else { window.alert("http request error"); return false; } http_request.send(content);} function processTextResponse() { if(http_request.readyState == 4) { if(http_request.status == 200) { alert(http_request.responseText); } else { alert("request error"); } }} function processXMLResponse() { if(http_request.readyState == 4) { if(http_request.status == 200) { alert(http_request.responseXML); } else { alert("request error"); } }}
时间: 2024-10-13 20:04:30