Ajax in the eyes of many programmers is a very complex or unfamiliar word, in fact, Ax is not complex, since the Ajax technology came out, the framework of the hype has been introduced, to get technology developers can not, Baidu Google also has many examples, most of them are very complex.
In fact, in terms of web development, Ajax technology is just a tie, there is no need to the cart before the horse, is a page optimization technology, that is, how to optimize our web page is Ajax plays. Let me give you a very simple example to meet most of the business requirements. (Of course, if it's a very professional page request, you can refer to those complex frameworks)
The first step: write a background of the "interface", which can be implemented in any language, as long as you can return the HTTP message can be, I here with webwork background code for example
Public String Hotweek () throws exception{
HttpServletResponse response = Servletactioncontext.getresponse ();
Response.setcontenttype ("Text/xml; charset=gb2312 ");
PrintWriter out = Response.getwriter ();
StringBuffer inserthothtml = new StringBuffer ();
Inserthothtml.append ("
Cellspacing=0> ");Inserthothtml.append ("
");Inserthothtml.append ("
| ");Inserthothtml.append ("
");Inserthothtml.append ("
");
Out.print (Inserthothtml.tostring ()); Returns an HTTP message with a table
return null;
}
No Java friends do not have to do this, as long as you remember, can return an HTTP message, such as a static Web page can also.
Step Two: Add the following JavaScript code to the page
var xmlHttp;
function Createxmlhttprequest () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
}
}
function Startajaxrequest (Method,async,actionurl,data, InvokeMethod) {
Createxmlhttprequest ();
Xmlhttp.open (method, ActionURL, async);
Xmlhttp.onreadystatechange = Handlestatechange;
Xmlhttp.send (data);
function Handlestatechange () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
var nodeId = Xmlhttp.responsetext;
if (nodeid== ' nopermission ') {
Alert (' You do not have permission to access this operation! ');
}else if ((Falseindex = Nodeid.indexof ("false| |"))! =-1) {
Alert (' operation failed, possible cause: ' + nodeid.substring (
Falseindex+7, nodeid.length) + "!");
}else if (nodeid== ' false ') {
Alert (' Operation failed, please contact admin! ');
}else ... {
if (InvokeMethod = = undefined) {
GetResult (NODEID);
} else {
InvokeMethod (NODEID);
}
}
}
}
}
}
We want to use is Startajaxrequest (Method,async,actionurl,data, InvokeMethod) This method, concrete implementation, also can not ignore, for IE and Firefox can be compatible, methods Either get, or post,async we are generally set to true on it, data used to pass to the background, InvokeMethod refers to the background after the return of data, the foreground automatically call what method, if empty, then the default call InvokeMethod ( NodeId) This method.
$ (document). Ready (function () {
var actionurl = "./provider!hotweek.action";
$ (' body '). HTML (" ... in Page loading");
Startajaxrequest ("Get", True,actionurl, "");
});
Here I am using jquery, a very useful JavaScript framework.
"./provider!hotweek.action" is the address of the HTTP message we get, we can use such as mytable.asp mytable.php as long as the return of the HTTP message is in line with the XML rules of the HTML language.
Step three: Back to the background after the processing method
function GetResult (nodeId) {
$ (' body '). HTML (NODEID);
}
To sum up, as long as startajaxrequest ("Get", "True,actionurl,") such a method can meet most of the business needs of Ajax, there is no need to make it so complicated.