Small Ajax JavaScript library [closed]

Here you go, pretty simple:

function createXHR()
{
    var xhr;
    if (window.ActiveXObject)
    {
        try
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            alert(e.message);
            xhr = null;
        }
    }
    else
    {
        xhr = new XMLHttpRequest();
    }

    return xhr;
}

Documentation is here

Example:

var xhr = createXHR();
xhr.onreadystatechange = function()
{
    if (xhr.readyState === 4)
    {
        alert(xhr.responseText);
    }
}
xhr.open('GET', 'test.txt', true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send()

Update:

In order to do cross-domain scripting, you’ll either have to call out to a local server-side proxy (which reads and echo’s the remote data), or, if your remote service returns JSON, use this method:

var s = document.createElement('script')
s.src="https://stackoverflow.com/questions/3470895/remotewebservice.json";
document.body.appendChild(s);

Since JSON is essentially a JavaScript object or array, this is a valid source. You theoretically should then be able to call the remote service directly. I haven’t tested this, but it seems to be an accepted practice:

Reference: Calling Cross Domain Web Services in AJAX

Leave a Comment