Very simple Ajax request instance attached source _ajax related

Source: Internet
Author: User
Tags html form http post

Ajax is not a new programming language, but a technology for creating better, faster, and more interactive Web applications. With Ajax, you can use JavaScript's XMLHttpRequest object to communicate directly with the server. You can exchange data with a WEB server without overloading the page. In the example of this article, we will demonstrate how a Web page communicates with a Web server when the user enters data into a standard HTML form.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

The following is a detailed description of the functions of each JS function.
createxmlhttprequest ()Used to create a XMLHttpRequest object.
Because IE implements XMLHttpRequest as an ActiveX object, other browsers (Ff/safari/opera) implement it as a local JavaScript object. Because of these differences, JavaScript code must contain the relevant logic.
createquerystring ()Used to organize parameters, the AJAX request to pass the parameters of the collation into a certain format.
If the transfer of Chinese or non-ASCII characters must be URL-coded, this example uses the encodeURIComponent () function of JS to encode the parameter URL.
Dorequestusingget ()Sends a request to the server as an HTTP get and passes parameters.
The XMLHttpRequest object's open () method specifies the request that will be issued. The open () method takes 3 parameters: one is a string indicating the method used (usually a get or post); one is a string representing the URL of the target resource; one is a Boolean value, but the request is asynchronous.
Get requests, the parameters passed are written to the URL parameter of the Open method, at which point the parameter of the Send method is null.
In some cases, some browsers cache the results of multiple XMLHttpRequest requests in the same URL. If the response to each request is different, this leads to bad results, and appending the current timestamp to the end of the URL ensures the uniqueness of the URL, thus avoiding the browser caching results.
The server-side code for this example uses ASP.net (C #).
dorequestusingpost ()Sends a request to the server as an HTTP post and passes parameters.
To ensure that the method specified in open () is post, you need to set Content-type header information, and simulate the HTTP POST method to send a form so that the server knows how to handle the uploaded content. You must first call the Open method before you can set header information.
Parameters must be passed using the Send method. The submit format of the parameter is the same as the URL in the Get method.
Handlestatechange ()Ajax callback function.
For the XMLHttpRequest object, the onReadyStateChange property stores a pointer to the callback function. This callback function is invoked when the internal state of the XMLHttpRequest object changes.
parseresults ()Process the response results.

The above is a very simple example of Ajax request, Welcome to learn, download Ajax request instance.

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.