AJAX Get and Post methods (sending requests to the server)

Source: Internet
Author: User
Tags unique id

To send the request to the server, we use the open () and send () methods of the XMLHttpRequest object:

Method Description
Open (method,url,async)

Specifies the type of request, the URL, and whether the request is processed asynchronously.

  • method: type of request; GET or POST
  • URL: The location of the file on the server
  • Async: True (asynchronous) or false (synchronous)
Send (string)

Sends the request to the server.

  • string: only for POST requests

GET is simpler and faster than POST, and can be used in most cases.
However, use the POST request in the following cases:
Unable to use cache file (update file or database on server)
Send a large amount of data to the server (POST has no data volume limit)
Post is more stable and more reliable than GET when sending user input with unknown characters

Get Method Example:

1 <HTML>2 <Head>3 <Scripttype= "Text/javascript">4 functionLoadxmldoc ()5 {6 varXMLHTTP;7 if(window. XMLHttpRequest)8   {//code for ie7+, Firefox, Chrome, Opera, Safari9 XMLHTTP=NewXMLHttpRequest ();Ten   } One Else A   {//code for IE6, IE5 - XMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); -   } the Xmlhttp.onreadystatechange=function() -   { -   if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) -     { + document.getElementById ("mydiv"). InnerHTML=Xmlhttp.responsetext; -     } +   } A Xmlhttp.open ("GET","/ajax/demo_get.asp",true); at xmlhttp.send (); - } - </Script> - </Head> - <Body> -  in <H2>Ajax</H2> - <Buttontype= "button"onclick= "Loadxmldoc ()">Request data</Button> to <DivID= "Mydiv"></Div> +  - </Body> the </HTML>

In the example above, it is possible to get the results of the cache.
To avoid this situation, add a unique ID to the URL:

1 xmlhttp.open ("GET", "demo_get.asp?t=" + math.random (), true);

2 xmlhttp.send ();

If you want to send information through the GET method, add information to the URL:

1 xmlhttp.open ("GET", "Demo_get2.asp?fname=bill&lname=gates", true);

2 xmlhttp.send ();

Post Method Ibid.

Example:

1 xmlhttp.open ("POST", "demo_post.asp", true);

2 xmlhttp.send ();

If you need to POST data like an HTML form, use setRequestHeader () to add an HTTP header. Then specify the data you want to send in the Send () method:

Method Description
setRequestHeader (header,value)

Adds an HTTP header to the request.

  • header: Name of the specified header
  • Value: The values of the specified header

code example:

1 xmlhttp.open ("POST", "ajax_test.asp", true);

2 xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

3 xmlhttp.send ("Fname=bill&lname=gates");

Asynchronous-True or False?
AJAX refers to asynchronous JavaScript and XML (asynchronous JavaScript and XML).
The async parameter of the open () method must be set to True if the XMLHttpRequest object is to be used for AJAX, or false.

When using Async=false, do not write the onreadystatechange function-put the code behind the Send () statement:

1 xmlhttp.open ("GET", "Test1.txt", false);

2 xmlhttp.send ();

3 document.getElementById ("mydiv"). Innerhtml=xmlhttp.responsetext;

AJAX Get and Post methods (sending requests to the server)

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.