Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AJAX do antying if XMLHttpRequestObject is deleted and/or contains no value since it's also set to null?

So I'm reading a book on AJAX, and they are talking about using inner function as a way to handle multiple requests. I understand that, but in this bit of code they used, I don't understand how the variable XMLHttpRequestObject can still be used:

if(XMLHttpRequestObject) 
{
    XMLHttpRequestObject.open(“GET”, dataSource);

    XMLHttpRequestObject.onreadystatechange = function()
    {
        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) 
        {
            document.getElementById(“targetDiv”).innerHTML = XMLHttpRequestObject.responseText;
            delete XMLHttpRequestObject;
            XMLHttpRequestObject = null;
        }
    }

    XMLHttpRequestObject.send(null);
}

My first qualm is when they delete XMLHttpRequestObject and then, after it's supposedly deleted, they set it equal to null. Then after it supposedly deleted and set to null, they use the XMLHttpRequestObject.send(null); But how does it do anything when XMLHttpRequestObject is deleted and/or contains no value since it's also set to null?

like image 902
Justen Avatar asked Feb 24 '10 20:02

Justen


People also ask

Why do we use the XMLHttpRequest object in AJAX?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

What is XMLHttpRequest object in AJAX & Explain how can you create XMLHttpRequest object?

XMLHTTPRequest object is an API which is used for fetching data from the server. XMLHTTPRequest is basically used in Ajax programming. It retrieve any type of data such as json, xml, text etc. It request for data in background and update the page without reloading page on client side.

What is AJAX with appropriate functions explain how XMLHttpRequest object is created and is send to the server?

All modern browsers support the XMLHttpRequest object. The XMLHttpRequest object can be used to exchange data with a server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

How AJAX will work XMLHttpRequest and call back with suitable code?

Steps of AJAX Operation The XMLHttpRequest object is configured. The XMLHttpRequest object makes an asynchronous request to the Webserver. The Webserver returns the result containing XML document. The XMLHttpRequest object calls the callback() function and processes the result.


2 Answers

The onreadystatechange function doesn't execute immediately when it is created. As the name suggests, it is called when the state changes.

In this case the XMLHttpRequestObject is only deleted after XMLHttpRequestObject.readyState == 4 and XMLHttpRequestObject.status == 200, i.e. after the page is successfully received.

like image 87
Mark Byers Avatar answered Sep 22 '22 05:09

Mark Byers


My first qualm is when they delete XMLHttpRequestObject and then, after it's supposedly deleted, they set it equal to null.

Don't know why they're doing that. Setting to null seems sufficient to me, but perhaps this solves some obscure browser quirk.

Then after it supposedly deleted and set to null, they use the XMLHttpRequestObject.send(null);

no. the deleting takes place inside the event handler - this event handler function is not called until after the request is done, and the state of the xhr object changes, for example due to the server sending the response, or an error occurring in the communication.

Basically, the calling sequence is not the same as the declaration sequence. The calling sequence is:

XMLHttpRequestObject.open(“GET”, dataSource);
... //assign event handler so it can be called later on
XMLHttpRequestObject.send(null); 
... //request send, program continues


//separate context here, goes off when the readystate of the xhr changes
//due to server response or error:
function()
{
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) 
    {
        document.getElementById(“targetDiv”).innerHTML = XMLHttpRequestObject.responseText;
        delete XMLHttpRequestObject;
        XMLHttpRequestObject = null;
    }
}
like image 25
Roland Bouman Avatar answered Sep 21 '22 05:09

Roland Bouman