Ajax Programming :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script type="text/javascript" language="javascript">
// function to create an XMLHttpClient in a cross-browser manner
function getAJAXRequest() {
var xmlhttp=null;;
try {
// Mozilla / Safari / IE7
xmlhttp = new XMLHttpRequest();
} catch (e) {
// IE
var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP' );
var success = false;
for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
try {
xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
success = true;
break;
} catch (e) {}
}
if (!success) {
return null;
}
}
return xmlhttp;
}
function loadAjaxCall(){
var http_request=getAJAXRequest();
//open(method,url,async)
//Specifies the type of request, the URL, and if the request should be handled asynchronously or not.
//method: the type of request: GET or POST
//url: the location of the file on the server
//async: true (asynchronous) or false (synchronous)
var parameters="one="+FristVal+"&sec="+SecVal;
//Note: When you use async=false, do NOT write an onreadystatechange function - just put the code after the send() statement:
//Note: When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:
http_request.open('POST', "/projectName/SomeAction.do", false);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
// here the state is synch
// getting the response as responseText or responseXML
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
/*
* Suppose if Asynch we are have to set the readyState
// here the response is in ready state
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
*
*/
// onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes
//readyState :-
//Holds the status of the XMLHttpRequest. Changes from 0 to 4:
//0: request not initialized
//1: server connection established
//2: request received
//3: processing request
//4: request finished and response is ready
//status:-
//200: "OK"
//404: Page not found
}
</script>
<BODY>
</BODY>
</HTML>
No comments:
Post a Comment