簡體   English   中英

XMLHttpRequest() 不適用於 IE

[英]XMLHttpRequest() Not Working with IE

下面的腳本在 Firefox 和 Chrome 上運行良好,但我似乎無法使用它,即,嘗試了所有方法甚至降低了瀏覽器的安全性,看看是否阻止了它,但我仍然無法讓它工作。

function postData() {

    var http = new XMLHttpRequest();

    var url = "/scripts/remove_fr.php";

    var params = "";

    http.open("GET", url, true);



    //Send the proper header information along with the request

    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    http.setRequestHeader("Content-length", params.length);

    http.setRequestHeader("Connection", "close");



    http.onreadystatechange = function() { //Call a function when the state changes.

        if(http.readyState == 4 && http.status == 200) {



        }

    }



    http.send(params);



}



    $("#qwerty").click(function () {

      $('#qwerty').remove();

    });



</script>

在 IE7 下,它使用ActiveXObject對象而不是XMLHttpRequest所以你的代碼應該是這樣的:

function postData() {
    var http;

    if (window.XMLHttpRequest) {
         // code for IE7+, Firefox, Chrome, Opera, Safari
         http = new XMLHttpRequest();
    }
    else {
         // code for IE6, IE5
         http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "/scripts/remove_fr.php";
    var params = "";
    http.open("GET", url, true);

   //Send the proper header information along with the request
   http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http.setRequestHeader("Content-length", params.length);
   http.setRequestHeader("Connection", "close");
   http.onreadystatechange = function() { //Call a function when the state changes.
       if(http.readyState == 4 && http.status == 200) {

       }
   }
   http.send(params);
}

你能試試這段代碼嗎,這會根據瀏覽器獲取 XMLHttpRequest。

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

詳情請參考w3schools鏈接

您已經在使用 jQuery,因此請使用jQuery 的 AJAX 實用程序函數 不要嘗試自己動手; XMLHttpRequest API 既丑陋又煩人。

我想提供示例代碼,但現在您所擁有的只是:

$.get("/scripts/remove_fr.php");

這不是一個例子。 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM