[ASP. NET] teaches you how to learn Ajax step by step (1) using the get Method

Source: Internet
Author: User

I will not write any description about the Ajax concept. If you are not clear about it, you can go to Baidu to search for it. (Click here to enter)

Note: Ajax is notProgramming LanguageIs used to create better, faster, and more interactive web applications.Program.

What can ajxa do for us? Why do we need Ajax?

With the rapid development of technologies, user experience is becoming more and more important. In the face of a dry, no effect of the page, the user will not stay too much. Only those pages with good user experience can users spend some time browsing them.

Then, today's page views are a sign of the success of a website. To get more users, you must make the page meet users' habits and needs.

Through Ajax, your javascript can use the XMLHTTPRequest object of JavaScript to directly communicate with the server. With this object, your javascript can exchange data with the Web server without reloading the page. Ajax uses asynchronous data transmission (HTTP request) between the browser and the web server, so that the webpage can request a small amount of information from the server, rather than the whole page. In this way, the user's network is unstable, but partial refreshing does not affect the user's browsing or other operations on the page.

Several important attributes and methods of XMLHttpRequest:

Attribute:

Readystate: 0-(not initialized) The send () method has not been called; 1-(loaded) The send () method has been called and a request is being sent; 2-(Loading completed) after the send () method is executed, all the response content has been received; 3-(interaction) is parsing the response content; 4-(Completion) The response content is parsed and can be called on the client.

Onreadystatechange: storage function (or function name). This function is called whenever the readystate attribute changes.

Status :( 1) 200: "OK"; (2) 404: the page is not found.

Methods: void open (string, String, Boolean), void send (string), void setheader (string, string), string getResponseHeader (string ),

String getAllResponseHeaders (), void abort ().

There are several steps to use Ajax that must be familiar:

1. Create an XMLHTTPRequest object for asynchronous communication with the server. When creating this object, you must create different objects based on different browsers. The kernel based on IE browser is:

VaR XMLHttpRequest = new activexobject ("Microsoft. XMLHTTP"); orVaR XMLHttpRequest = new activexobject ("msxml2.xmlhttp"), which has many versions.

For other purposes, VAR XMLHttpRequest = new XMLHttpRequest ().

2. after creating an object, you need to use the open method for interaction settings, XMLHttpRequest. open ("get", URL, true); "get" indicates that the get method is used, and the input URL is the address for getting data or interacting. The third parameter, when the Boolean value is true, the server request is asynchronous, that is, the script executes the send () method without waiting for the server's execution results, but continues to execute the script.CodeWhen the Boolean value is false, the server request is synchronously executed, that is, the script executes the send () method and waits for the server to return the execution result. If the request times out during the waiting process, the script code will not be executed!

3. Specify a called function for onreadystatechange and assign it to the function name, for example, XMLHttpRequest. onreadystatechange = getfromhandler;

4. Enable the send Method for interaction. XMLHttpRequest. Send (null); because the get method is used, you do not need to use send to pass the parameter value here.

5. Implement the getfromhandler () function in step 3. Because the operation is performed only after the communication ends, first determine whether the readystate value is 4 and the status value is 200, and then perform the operation on the page.

 

 
Function getfromhandler (){
If(XMLHttpRequest. readystate =4){
If(XMLHttpRequest. Status =200){
Document. getelementbyid ("Result"). Value = XMLHttpRequest. responsetext;
}
}
}

The above figure shows how to send data from the background (XMLHttpRequest. responsetext) on the page.

You can use a general processing program to perform data operations. Here, you can process the data as needed and then return to the home page. simply, it is "Hello World.

1Public VoidProcessrequest (httpcontext context)
2{
3Context. response. contenttype ="Text/plain";
4Context. response. Write ("Hello World");
5}

When sending a large amount of data, we cannot simply send a line of characters. We need to construct an XML format to transfer large amounts of data. This makes the homepage easy to use, when obtaining information on the main interface
You can directly obtain the values of each node as in XML operations.

Document. getelementbyid ("text"). value = xmlhttprequest.responsexml.doc umentelement. firstchild. nodevalue;

In this way, the interaction between the home page and the background is completed, and the effect is to partially refresh the page to communicate with the background. The key part is to familiarize yourself with the process of using the XMLHTTPRequest object:

Ajax applications

Submit
1. XMLHttpRequest ----> request

Return Analysis
2. server ----> data <---- 3. Javascript

Let's attach two small examples, a common "Hello World", and a interaction with data. Of course, the reader cannot directly run it, because you don't have to have my local database, you only need to modify the settings to run properly. You have to leave some tests for the readers to complete!

This blog post is not well written. I hope you can give me more advice and I am sorry.

Click here to download

Like meArticleThenFollowMe! Everyone's support is my motivation!

 

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.