Javascript & Ajax

Performing GET and POST requests using Ajax

Ajax, Asynchronous JavaScript and XML, is not a programming language, is a group of technologies used on the client-side to send data to, or retrieve data from a server and update parts of a web page without reloading the whole page. Data is usually retrieved using the XMLHttpRequest object. JSON is often used instead of XML. more on wiki.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
function LoadDataInDiv(page, params, divid, method) {
	var http_request = false;
 
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
	      http_request.overrideMimeType('text/html');
	    }
	} 
 
	else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
	    }
	  }
 
	if (!http_request) {
		return false;
	}
 
  http_request.onreadystatechange=function(){
	  if (http_request.readyState==4){
	   if (http_request.status==200 || window.location.href.indexOf("http")==-1){
    document.getElementById(divid).innerHTML=http_request.responseText;
   }
  }
 }
 
  var symbol = "?";
 
  //make GET request
  if(method=="GET"){
	 http_request.open(method, page+symbol+params, true);
	 http_request.send(null);
  }
 
  //make POST request
  else if(method=="POST"){
	 http_request.open("POST", page, true);
	 http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 http_request.send(params);
  }
}