XMLHttpRequest()不使用IE

时间:2012-03-11 19:01:09

标签: javascript jquery xml ajax

下面的脚本可以正常使用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>

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

您可以尝试使用此代码,这会根据浏览器获取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

答案 2 :(得分:1)

您已经在使用jQuery了,所以请使用jQuery's AJAX utility functions!不要试图自己动手; XMLHttpRequest API很丑陋而烦人。

我想提供示例代码,但是现在你所拥有的只是:

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

这不是一个例子。 ;)