javascript中向服务器发送http请求借助的是XMLHttpRequest对象,其他一些库如JQuery对http的请求应该是在这个上面的封装,创建XMLHTPRequest对象用下面的语句:

function createXMLHttpRequest() {
 var xmlHttp;
 if (window.XMLHttpRequest) {
  xmlHttp = new XMLHttpRequest();
  if (xmlHttp.overrideMimeType)
   xmlHttp.overrideMimeType('text/xml');
 } else if (window.ActiveXObject) {
  try {
   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {
   }
  }
 }
 return xmlHttp;
}



发送get请求,并异步处理

    xmlHttp = createXMLHttpRequest();
    var url = "getfiledetail.jsp?fileid="+id;
    xmlHttp.open("GET", url, true);// 异步处理返回 
    xmlHttp.onreadystatechange = callback; 
    xmlHttp.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded;");
    xmlHttp.send();



发送post请求

    var url = "getNginxStatus";
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = getStatusBack;
    xmlHttp.setRequestHeader("Content-Type",
    "application/x-www-form-urlencoded;");
    xmlHttp.send(xml);

 

posted on 2013-01-29 07:51  醉意人间  阅读(1312)  评论(0编辑  收藏  举报