Runoob Tutorial ArchiveEnglish

AJAX – Send to server

AJAX - Send request to server


The XMLHttpRequest object is used to exchange data with the server.


Send request to server

To send a request to the server, we useXMLHttpRequestobject'sopen()andsend()method:

xmlhttp.open("GET","ajax_info.txt",true);

xmlhttp.send();

method description
open(method,url,async)

Specifies the request type, URL, and whether to process the request asynchronously.

  • method: The request type; GET or POST
  • url: File location on the server
  • async: true (asynchronous) or false (synchronous)
  • | | send(*string*) |

    Sends the request to the server.

    |

    GET or POST?

    Compared to POST, GET is simpler and faster, and can be used in most cases.

    However, use a POST request in the following situations:

    ytxdmm71yvt3zokkrpn4 scaled


    GET request

    A simple GET request:

    Instance

    xmlhttp

    .

    open

    (

    "

    GET

    "

    ,

    "

    /try/ajax/demo_get.php

    "

    ,

    true

    )

    ;

    xmlhttp

    .

    send

    (

    )

    ;

    Try »

    In the example above, you might get a cached result.

    To avoid this situation, please add a unique ID to the URL:

    Instance

    xmlhttp

    .

    open

    (

    "

    GET

    "

    ,

    "

    /try/ajax/demo_get.php?t=

    "

    Math

    .

    random

    (

    )

    ,

    true

    )

    ;

    xmlhttp

    .

    send

    (

    )

    ;

    Try »

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

    instance

    xmlhttp

    .

    open

    (

    "

    GET

    "

    ,

    "

    /try/ajax/demo_get2.php?fname=Henry&lname=Ford

    "

    ,

    true

    )

    ;

    xmlhttp

    .

    send

    (

    )

    ;

    Try it »


    POST request

    A simple POST request:

    instance

    xmlhttp

    .

    open

    (

    "

    POST

    "

    ,

    "

    /try/ajax/demo_post.php

    "

    ,

    true

    )

    ;

    xmlhttp

    .

    send

    (

    )

    ;

    Try »

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

    instance

    xmlhttp

    .

    open

    (

    "

    POST

    "

    ,

    "

    /try/ajax/demo_post2.php

    "

    ,

    true

    )

    ;

    xmlhttp

    .

    setRequestHeader

    (

    "

    Content-type

    "

    ,

    "

    application/x-www-form-urlencoded

    "

    )

    ;

    xmlhttp

    .

    send

    (

    "

    fname=Henry&lname=Ford

    "

    )

    ;

    Try »

    Method Description
    setRequestHeader(header,value)

    Adds an HTTP header to the request.

    • header: Specifies the name of the header
    • value: Specifies the value of the header
    • |

      url - file on the server

      of the open() methodurlThe parameter is the address of the file on the server:

      xmlhttp.open("GET","ajax_test.html",true);
      
      

      This file can be any type of file, such as .txt and .xml, or a server script file, such as .asp and .php (to execute tasks on the server before returning the response).


      Asynchronous - True or False?

      AJAX refers to Asynchronous JavaScript and XML.

      If the XMLHttpRequest object is to be used for AJAX, its open() method's async parameter must be set to true:

      xmlhttp.open("GET","ajax_test.html",true);
      
      

      For web developers, sending asynchronous requests is a huge improvement. Many tasks executed on the server are quite time-consuming. Before AJAX, this could cause the application to hang or stop.

      With AJAX, JavaScript does not have to wait for the server's response, but rather:


      Async=true

      When using async=true, specify the function to execute when the response is in the ready state of the onreadystatechange event:

      Instance

      xmlhttp

      .

      onreadystatechange

      =

      function

      (

      )

      {

      if

      (

      xmlhttp

      .

      readyState

      ==

      4

      &&

      xmlhttp

      .

      status

      ==

      200

      )

      {

      document

      .

      getElementById

      (

      "

      myDiv

      "

      )

      .

      innerHTML

      =

      xmlhttp

      .

      responseText

      ;

      }

      }

      xmlhttp

      .

      open

      (

      "

      GET

      "

      ,

      "

      /try/ajax/ajax_info.txt

      "

      ,

      true

      )

      ;

      xmlhttp

      .

      send

      (

      )

      ;

      Try it »

      You will learn more about onreadystatechange in later chapters.


      Async = false

      If you need to use async=false, change the third parameter in the open() method to false:

      xmlhttp.open("GET","test1.txt",false);
      
      

      We do not recommend using async=false, but it is acceptable for some small requests.

      Remember that JavaScript will wait for the server response to be ready before continuing execution. If the server is busy or slow, the application will hang or stop.

      **Note:** When using async=false, do not write an onreadystatechange function - place the code after the send() statement:

      Instance

      xmlhttp

      .

      open

      (

      "

      GET

      "

      ,

      "

      /try/ajax/ajax_info.txt

      "

      ,

      false

      )

      ;

      xmlhttp

      .

      send

      (

      )

      ;

      document

      .

      getElementById

      (

      "

      myDiv

      "

      )

      .

      innerHTML

      =

      xmlhttp

      .

      responseText

      ;

      Try it »