javascript - XMLHttpRequest() 不适用于 IE

标签 javascript jquery xml ajax

下面的脚本在 Firefox 和 Chrome 上运行良好,但我似乎无法让它在 ie 上运行,尝试了所有甚至降低浏览器安全性的方法,看看是否阻止了它,但我仍然无法让它运行.

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

关于javascript - XMLHttpRequest() 不适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657942/

相关文章:

javascript - 两个 javascript 表单提交的 IE10 下载问题

javascript - History.js 和 url 编码

javascript - 在javascript中定位数组中的最后一项

javascript - THREE.js 制作背景中有其他对象的透明对象

XMLStarlet 不删除元素

java - 如何使用Java正确压缩和转换TiledMap数据?

java - 使用Eclipse,layout.xml文件上的TabLayout错误

javascript - 使用下拉列表过滤后如何保留多选框中的选定值

javascript - 如何在复选框选中/取消选中事件上发送发布请求

javascript - jQuery 切换脚本