Search This Blog

Friday, September 4, 2009

Three technology to handle Simple Ajax

Recently I have used 3 technology in one portal project on requirement basis for handling ajax calls , explaining all three ways to make ajax calls here . I found that JQuery is most effective and easy to use :

1. Using native ajax :

//Do the AJAX call
if (window.XMLHttpRequest) {

// Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert("Server Communication Problem\n"+e);
}
req.send(null);
} else if (window.ActiveXObject) {
// IE

req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange=processStateChange;
req.open("GET", url, true);
req.send();
}
}

function processStateChange() {

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response

//Split the text response into Span elements
spanElements =
splitTextIntoSpan(req.responseText);

//Use these span elements to update the page
replaceExistingWithNewHtml(spanElements);

} else {
alert("Problem with server response:\n "
+ req.statusText);
}
}
}

2. Using JQuery :

 $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
},
error : function() {}
});


3 Using Smart client : Use data source with Json Or XML

isc.DataSource.create({
ID:"contacts",
dataFormat:"json",
dataURL:employee.php",
fields:[
{name:"name"},
{name:"email"},
{name:"organization"},
{name:"phone"},
{name:"street", valueXPath:"address/street" },
{name:"city", valueXPath:"address/city" },
{name:"state", valueXPath:"address/state"},
{name:"zip", valueXPath:"address/zip" }
]
});

No comments:

Post a Comment

Thanks for your comment, will revert as soon as we read it.

Popular Posts