js xmlHttp對象IE瀏覽器和非IE瀏覽器建立方法

來源:互聯網
上載者:User

js xmlhttp對象ie瀏覽器和非ie瀏覽器建立方法


    例子:建立一個xmlhttp對象,並向伺服器請求一個xml文檔,返迴文檔後顯示。下面分別就ie和非ie進行執行個體講解

    1) ie瀏覽器使用activexobject方式建立xmlhttp對象:

   

   var xmlhttpreq = new activexobject("msxml2.xmlhttp.3.0");

      xmlhttpreq.open("get", "http://localhost/test.xml", false);

      xmlhttpreq.send();

      alert(xmlhttpreq.responsetext);

    2) 非ie瀏覽器使用xmlhttprequest方式建立xmlhttp對象:

     

 var xmlhttpreq = new xmlhttprequest();

      xmlhttpreq.open("get", "http://localhost/test.xml", false);

      xmlhttpreq.send();

      alert(xmlhttpreq.responsetext);

  2.2 建立完xmlhttp對象以後,因為它是一套api,所以它有很多的方法和屬性,如上面用到的open()、send()、responsetext。

    xmlhttp對象的代碼處理方式比較固定。因此下面要做的就只是按例子順序進行理解就可以了。

下面看一款完整的執行個體

js 相容ff activexobject建立xmlhttp方法
*/

function createxmlhttpobject()
 {
  var xobject = null;
  try
  {
   xobject = new activexobject("msxml2.xmlhttp.4.0");
  }
  catch (e)
  {
   try
   {
    xobject = new xmlhttprequest();
    if(xobject.overridemimetype){
     xobject.overridemimetype('text/xml');
    }
   }
   catch (e)
   {
    try
    {
     xobject = new activexobject("msxml2.xmlhttp");
    }
    catch (e)
    {
     try
     {
      xobject = new activexobject("microsoft.xmlhttp");
     }
     catch (e)
     { 
      alert("error: unable to create xml http object!");
     }
    }
   }
  }
  return xobject;
 }
 
 function initrequest(){
  request = createxmlhttpobject();
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.