Deep understanding of Ajax get and post requests _javascript tips

Source: Internet
Author: User

1.get Request

function ()
 {
    //Request time from Server
    //1. To create an asynchronous object (small browser)
    var xhr = new XMLHttpRequest ();
    2. Set parameters, true to use asynchronous mode
    Xhr.open ("Get", "Gettime.ashx?name= Mr Jing", true); 
    3. Let get requests do not get cached data from the browser
    xhr.setrequestheader ("if-modified-since", "0");
    3. Set callback function
    Xhr.onreadystatechange = function ()
    {
          //3.1 when the response message is fully received and the response status code is 200
          if ( Xhr.readystate = = 4 && xhr.status =
          {
            //3.2 get corresponding report stylistic content
            var res = xhr.responsetext;
            Alert (res);
          }
    ;
    4. Send asynchronous request
    xhr.send (null);
}

2.post Request

function ()
 {
    //Request time from Server
    //1. To create an asynchronous object (small browser)
    var xhr = new XMLHttpRequest ();
    2. Set parameter
    xhr.open ("Post", "Gettime.ashx", true);
    3. Set the encoding format of the request report style (set to the form default encoding format)
    xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
    4. Set callback function
    Xhr.onreadystatechange = function ()
    {
        //3.1 when the response message is fully received and the response status code is 200
        if ( Xhr.readystate = = 4 && xhr.status =
       {
         //3.2 get corresponding report stylistic content
          var res = xhr.responsetext;
          Alert (res);
       }
     ;
     5. Send asynchronous Request "Name=mr Jing"
     //5.1 Format: Direct stitching string key=value&key1=value2
     xhr.send ("Name=mr jing &age=18");
 

The above in-depth understanding of the Ajax get and POST request is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.