Ajax
There's been a technical term on the web for the last two years. That's what Ajax is, I've been doing JavaScript related applications I've heard about it, but I haven't noticed it, and then I looked at it and said my understanding. Ajax is the abbreviation for asynchronous JavaScript and XML (although the inventor of its name, Jesse James Garrett, does not recognize it as the acronym for that string of words), See the name. This is an application of asynchronous JavaScript and XML. To tell the truth, I quite dislike this noun, the reason is very simple, it is not what new technology, but hype out a term. And its content is called Microsoft's immature, functionally not perfect XMLHTTP object to read remote XML documents just. It was later taken as a claim by other browsers. No more nonsense, let's talk about some of the necessary code for this thing.
There's been a technical term on the web for the last two years. That's what Ajax is, I've been doing JavaScript related applications I've heard about it, but I haven't noticed it, and then I looked at it and said my understanding.
Ajax is the abbreviation for asynchronous JavaScript and XML (although the inventor of its name, Jesse James Garrett, does not recognize it as the acronym for that string of words), See the name. This is an application of asynchronous JavaScript and XML.
To tell the truth, I quite dislike this noun, the reason is very simple, it is not what new technology, but hype out a term. And its content is called Microsoft's immature, functionally not perfect XMLHTTP object to read remote XML documents just. It was later taken as a claim by other browsers.
No more nonsense, let's talk about some of the necessary code for this thing.
The following is an example of a typical XMLHTTP object declaration instance:
/* Create A new XMLHttpRequest object to talk to the WEB server * *
var xmlHttp = false;
/* @cc_on @*/
/* @if (@_jscript_version >= 5)
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (E2) {
XmlHttp = false;
}
}
@end @*/
if (!xmlhttp && typeof xmlhttprequest!= ' undefined ') {
XmlHttp = new XMLHttpRequest ();
}
Oh! Do you feel like defining an object, why do you write so much? That's right! If it's just a single browser, there's really no need to write so much, but ... The nightmare began ... We have to deal with is not a single browser, but a wide variety of strange. We have no choice but to allow these messy browser clients to adapt to our products, we have compromised.
Then there's the call to the instance (see JavaScript documentation for details, and XMLHTTP's documentation), and thankfully the various browsers are more consistent with the object's methods and properties.
Properties of the XMLHTTP object
onReadyStateChange This is an event that when you invoke the object in an asynchronous manner, the object invokes the event when the read state changes. He is an attribute, and you can assign it a function pointer.
ReadyState read state. Used to determine the current read state, he has four values:
(0) The status code that is not initialized when an object is created but not initialized with the Open method
(1) The status code that is generated when the object is created while the Send method is not invoked
(2) The status code that is generated when the request is terminated, but not available
(3) The status code generated during the reading of data in the communication
(4) The completion of all the data read from time to time generated by the status code
Responsebody get a piece of data
Responsestream gets a data stream
ResponseText get a text document
Responsexml get an XML document
Status gets the request status of HTTP
StatusText to obtain HTTP request status information
Methods of XMLHTTP Objects
Abort cancel a request
getAllResponseHeaders gets all the HTTP header information
getResponseHeader gets the data in the HTTP header information
Open Initialization Object instance
Send sends a request
setRequestHeader set HTTP header information for the request
In fact, the method attribute is not much also not difficult to grasp, we look at one of its examples can be easily mastered
The following is an example of a call:
Var xmlhttp=null;
function Postorder (xmldoc)
{
var xmlhttp = new ActiveXObject ("Msxml2.xmlhttp");
XMLHTTP. Open ("POST", "http://myserver/orders/processorder.asp", false);
xmlhttp.onreadystatechange= Handlestatechange;
XMLHTTP. Send (xmldoc);
mybutton.disabled = true;
}
Function Handlestatechange ()
{
if (xmlhttp.readystate = = 4)
{
mybutton.disabled = false;
alert ("result =" + Xmlhttp.responseXML.xml);
}
}
This is, of course, a development model, not a new development model, and the individual thinks he appears synchronized when the XMLHTTP object appears (at least I've been using this pattern for a long time before the term comes up). The advantage of this is that we can communicate with the data server without refreshing the current page, and the amount of communication is reduced. The reason is simple, because it only returns data, rather than the server repeatedly generating a large number of page formatting code. We can completely download the entire station template to the local, share the burden of server-generated pages. And can make the client's browsing more fluent, users can also do a lot of fun, and in the previous development model this is difficult to achieve the function.
But any development pattern has its drawbacks, what is the disadvantage of Ajax, in fact, its shortcomings also led to from its emergence to the current epidemic in the middle of a long time. It is difficult to develop. Have you ever imagined debugging a JavaScript the most senior programmer is still using alert? Have you ever imagined that an unknown error in a certain state does not prompt the code to make an error, and even if prompted, does not indicate the location of the error and the object? Have you ever imagined that there are no perfect code hints? Do you have the imagination to face a plethora of browser compatibility issues? Unfortunately, we all do, because there is not a perfect integration environment for debugging, and almost all JavaScript programmers are using the most common text editors to accomplish these complex tasks.
It's a nightmare, indeed a real nightmare that exists around us, and thankfully, with the momentum of Ajax, most developers are working on this problem and there are a lot of javascript frameworks coming up. Just like ASP. NET development way, gradually appeared.
Perhaps Ajax's prospects are optimistic, and maybe it's a never-ending nightmare ...
Let's talk about the prerequisites for this development model.
First: You should be proficient in mastering html,css,xml. If this is not clear to you, trouble yourself to become familiar with it, or you will be unable to do anything.
Second: You have to master a browser client programming language, no matter what you are JavaScript or VBScript or other PerlScript and so on, must master a door, and skilled. Why are you skilled? Well, the reason is very simple, because this is a scripting language, because it has a simple syntax semantics definition rules, so you can encounter the more difficult to solve the problem. It is as if the assembly is always so difficult to master, like Weiqi, the simpler the rules, the more the chess changes.
Third: You should be familiar with the Html,css,xml browser to provide you with the document object, that is, DOM, otherwise you can class, because unfamiliar with these content, I can responsibly tell you that Ajax is useless to you, give up.
Four: You need to be familiar with the properties and methods of XMLHTTP, or you may say: Cut! Those are the ones! ~ ~ Oh, browsers provide the object is not the same oh, is the same browser to provide the relevant objects are not the same, not to mention the plethora of browsers?
V: A server-side language to provide XML data, and the database-related SQL language that might be used, which is also necessary (unless you have access to read data). I will not do a detailed introduction, the choice is indeed a lot, asp,asp. Net,php,cgi,jsp ... Too much
You need to be prepared to do it.
First: You may not have the opportunity to meet the more comfortable programming environment, but the bread will have ...
Second: You may have almost no convenient debugging environment, but Microsoft's script debug environment
Third: You may have to face the challenges of many browsers, including some of the differences in Html,css,xml, as well as the nuances of JScript and JavaScript, and of course the more headaches are the many different DOM objects and the more different browsers objects
If you are satisfied with these conditions, I can only silently wish you good luck ...