Sending an AJAX Request - Can't get to work

Posted by user357944 on Stack Overflow See other posts from Stack Overflow or by user357944
Published on 2010-06-03T21:53:59Z Indexed on 2010/06/03 21:54 UTC
Read the original article Hit count: 137

Filed under:
|
|
|

I'm trying to make an AJAX GET request, but I simply cannot get it to work. I want to retrieve the HTML source of example.com. I've previously used JQuery to send AJAX requests, but I use JQuery only for its AJAX capabilities so it's a waste to include the 30KB file for one task. What is it that I'm doing wrong?

<script type="text/javascript">

var XMLHttpArray = [
    function() {return new XMLHttpRequest()},
    function() {return new ActiveXObject("Msxml2.XMLHTTP")},
    function() {return new ActiveXObject("Msxml2.XMLHTTP")},
    function() {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject(){
    var xmlhttp = false;
    for(var i=0; i<XMLHttpArray.length; i++){
            try{
                    xmlhttp = XMLHttpArray[i]();
            }catch(e){
                    continue;
            }
            break;
    }
    return xmlhttp;
}
function AjaxRequest(url,method){
    var req = createXMLHTTPObject();
    req.onreadystatechange= function(){
            if(req.readyState != 4) return;
            if(req.status != 200) return;
            return req.responseText;
  }
    req.open(method,url,true);
    req.send(null);
}

function MakeRequst(){
var result=AjaxRequest("http://example.com","get");
alert(result);
}
</script>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX