I am confused right now, and there will be test about ajax tommorrow..

And i bumped into this getXMLHTTP() function in one of the code that my professor gave me and i not sure what it does exactly.I mean because i also see in the other code, that people are using getXMLHttpRequest() in their ajax code and this make me more confused.

I have tried finding it on the net but theres no one yet given explanation on this. So i hope someone could give me explanation on this.

By the way, below this is the example of code that using this function :

function getInfo(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		req.onreadystatechange = function()
		{
			if (req.readyState == 4) 
			{			
				if (req.status == 200)
				{						
					document.getElementById("lect").innerHTML=req.responseText;						
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL , true);
		 req.send(null);
	}			
}

thank you in advance :)

Dani AI

Generated

getXMLHTTP() is not part of the browser API — it is a user-defined helper function that most example code provides to hide how an XMLHttpRequest object is created (and, in very old examples, to fall back to an ActiveXObject for ancient IE). already spotted that it was custom, and was right to describe why people wrap instantiation with try/catch: the thing being created is the standard XMLHttpRequest object. (developer.mozilla.org)

Modern guidance is to stop inventing small factory helpers for XHR and use the Fetch API instead. Fetch is promise-based and generally simpler; it resolves to a Response object and requires explicit checks of response.ok or response.status for HTTP errors (fetch does not reject automatically on 4xx/5xx). A minimal replacement for an XHR-based helper looks like this:

fetch(url)
  .then(resp => {
    if (!resp.ok) throw new Error('HTTP ' + resp.status);
    return resp.text();
  })
  .then(html => document.getElementById('result').innerHTML = html)
  .catch(err => console.error('Request failed:', err));

The Fetch API is the modern replacement for XHR and is the recommended starting point for new code; see the Fetch docs for details and patterns. (developer.mozilla.org)

Practical notes: legacy code that calls getXMLHTTP() will either define that helper in a separate script block or expect a library to provide it — look for its definition before using it. Common runtime issues are missing definition, script load order, same-origin/CORS restrictions, and misuse of XHR ready-state/status checks; XHR still offers upload/download progress events that Fetch does not expose in the same simple way, so keep XHR if progress events are required. (developer.mozilla.org)

oouh2...i am so sorry...
actually getXMLHTTP() is the function that being made...
haha..sory for this...
anyway thanks :)

The XMLHTTP object is the core of AJAX functionality. It's essentially a way to call a web page roundtrip from within code. Depending on your browser, the exact syntax to call the XMLHTTP object differs. It might be as simple as:

new XMLHttpRequest

Or you might need to use an ActiveX object:

new ActiveXObject("Microsoft.XMLHTTP")

What's common in script is to declare a function that has try/catch blocks for the various ways of instantiating the XMLHTTP object, and most sample code out there defines that function as getXMLHTTP().

So in your code snippet, we see a function named getXMLHTTP() called, but we don't see that function actually defined.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.