Werken met AJAX

Hier zal ik mijn eigen code plaatsen.
Rudy Mas
Site Admin
Berichten: 31
Lid geworden op: 27 mar 2015, 15:01
Locatie: Hasselt, Belgium
Contacteer:

Werken met AJAX

Bericht door Rudy Mas » 11 apr 2016, 11:18

Handige manier om data op te halen van de server zonder de hele pagina opnieuw te moeten laden.

Code: Selecteer alles

// Read DATA on server and dynamically put it on page at ID ...
function loadAJAX(url, textID, params, HttpMethod)
{
	var AjaxHttp;
	
	if (!HttpMethod)
	{
		HttpMethod = "GET";
	}
	
	// branch for native XMLHttpRequest object
	try
	{
		AjaxHttp = new XMLHttpRequest();
	}
	catch (e)
	// branch for IE/Windows ActiveX version
	{
		try
		{
			AjaxHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				AjaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX.");
				return true;
			}
		}
	}

	if (AjaxHttp)
	{
		AjaxHttp.onreadystatechange = function ()
			{
				// only if AjaxHttp shows "complete"
				if (AjaxHttp.readyState == 4)
				{
					// only if "OK"
					if (AjaxHttp.status == 200)
					{
						var response = AjaxHttp.responseText;
						var page = document.getElementById(textID);
						
						page.innerHTML = response;
					}
					else
					{
						alert("There was a problem retrieving the data through AJAX:\n" + AjaxHttp.statusText);
						return true;
					}
				}
			}
		AjaxHttp.open(HttpMethod, url, true);
		AjaxHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		AjaxHttp.send(params);
	}
	return false;
}
Groetjes,
Rudy Mas

Plaats reactie