Protopyte. js ajax

Source: Internet
Author: User
Document directory
  • HTML file

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:

  1. 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.

  2. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.