Document directory
In the previous blog, I used the original XMLHttpRequest for a simple test. Below I will use prototype. js to implement similar tests.
HTML file
<HTML>
<Head>
<Title> test </title>
<SCRIPT src = "prototype. js"> </SCRIPT>
</Head>
<Body style = "background-color: # cccccc">
<SCRIPT>
Function showmessage (){
New Ajax. Request ('getmessage. py', {method: "Get", oncomplete: oncomplete });
}
Function oncomplete (Transport ){
A = transport. responsexml. getelementsbytagname ("A") [0];
$ ('Message'). innerhtml = A. firstchild. nodevalue;
}
</SCRIPT>
<Span id = "message" style = "display: inline"> message </span>
<Div> <a id = "demopanellink" href = "javascript: showmessage ();"> click here </a> </div>
</Body>
</Html>
The main changes are:
- Create an instance of AJAX (which is defined in prototype. JS) and input the URL to be accessed and the called option parameters.
By default, Ajax will send services to the background in post mode. Here, because the test is very simple, I need to use the get method. Set Method: "get ". This is used for internal Ajax identification, so it is not "get ". Then input a corresponding processing method when the transfer is complete. Here is oncomplete. Once the processing is complete, Ajax will automatically call the oncomplete method and pass in the XMLHTTPRequest object created during processing.
- Defines an oncomplete processing method. The parameter is the XMLHTTPRequest object to be passed in.
Therefore, you can use this method to parse data.
The result of the above example is that when you click the link, the text above it will change from message to Hello, world. Please note that this Hello world comes from the background.