Friday, February 13, 2015

HTTP Request with Javascript

<script type="text/javascript">

var oReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");

function handler()
{
    if (oReq.readyState == 4 /* complete */) {
        if (oReq.status == 200) {
            document.getElementById("myDiv").innerText = oReq.responseText;
        }
    }
}

function SendRequest()
{
    var vURL = "http://IdolApp:12345/data/data.xml";

    if (oReq != null) {
        oReq.open("GET", vURL, true);
        oReq.onreadystatechange = handler;
        oReq.send();
    }
    else {
        window.console.log("AJAX (XMLHTTP) not supported.");
    }
}

</script>
<input type="button" id="btnSubmit" value="Go" onclick="SendRequest()"/>
<br/>
<div id="myDiv" >:o)</div>
Ref: https://msdn.microsoft.com/en-us/library/ie/ms535874(v=vs.85).aspx

Note:
Initialize the oReq with following to make it IE and Chrome compatible:
var oReq;

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

No comments:

Post a Comment

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...