最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHttpRequest() with IE - Stack Overflow

programmeradmin1浏览0评论

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.

<script type="text/javascript">
  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>

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.

<script type="text/javascript">
  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>
Share Improve this question edited Sep 9, 2024 at 0:20 David Refoua 3,6174 gold badges33 silver badges57 bronze badges asked Mar 11, 2012 at 19:01 CookiimonstarCookiimonstar 4212 gold badges7 silver badges18 bronze badges 1
  • IE version? Error? What is not working? What is happening? – epascarello Commented Mar 11, 2012 at 19:07
Add a comment  | 

3 Answers 3

Reset to default 12

Below IE7, it uses ActiveXObject objects instead of XMLHttpRequest So your code should be like this:

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);
}

You may try this code, this gets the XMLHttpRequest based on the browser.

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 to the link for details w3schools

You're already using jQuery, so use jQuery's AJAX utility functions! Don't try to roll your own; the XMLHttpRequest API is ugly and annoying.

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. ;)

发布评论

评论列表(0)

  1. 暂无评论