简体   繁体   English

XMLHttpRequest() 不适用于 IE

[英]XMLHttpRequest() Not Working with IE

The script below is working fine with Firefox and Chrome but I cant seem to get it to work with ie, tried everything even lowered the security on my browser to see if was that blocking it, but i still cant get it to work.下面的脚本在 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>

Below IE7, it uses ActiveXObject objects instead of XMLHttpRequest So your code should be like this:在 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);
}

Can you try this code , this gets the XMLHttpRequest based on the browser.你能试试这段代码吗,这会根据浏览器获取 XMLHttpRequest。

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

Please refer link for details w3schools详情请参考w3schools链接

You're already using jQuery, so use jQuery's AJAX utility functions !您已经在使用 jQuery,因此请使用jQuery 的 AJAX 实用程序函数 Don't try to roll your own;不要尝试自己动手; the XMLHttpRequest API is ugly and annoying. XMLHttpRequest API 既丑陋又烦人。

I'd like to provide sample code, but right now what you've got amounts to simply:我想提供示例代码,但现在您所拥有的只是:

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

which isn't much of an example.这不是一个例子。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM