Tuesday, 14 June 2016

Easy Learn AJAX- Beginners AJAX Tutorials Part 4- Send Request to a Server

Hi Friends

In this section how to send request to a server.  last section we create XMLHttpRequest Object in successfully. that object is xhhtp.

Ok fine! 

To send request to a server we can use following methos of XMLHttpRequest object like xhhtp

1. Open()
2. Send()

Basic Syntax of above methods

xhttp.open()
Note:

  • A small request than we can use Async="False" Method
  • A Husge request the we can use Async="True" Method
  • If we can use Async="True" then Must Write OnreadyStateChange Event
  • when Async="False" Then do not write OnreadyStateChange Event



xhttp.send()

This method we can use same like that..

Example 1 : Async is False method
<head>
<script>
function myfunction()
{
    var xhttp;
      if (window.XMLHttpRequest) 

      {
      xhttp = new XMLHttpRequest();   // Modern browser
      } 

      else
      {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP");  // Older browser 
      }

xhttp.open("POST""Customer.php"false);
xhttp.send();
}
</script>
</head>

Example 2 : Async is True method

<head>
<script>
function myfunction()
{
    var xhttp;
      if (window.XMLHttpRequest) 

      {
      xhttp = new XMLHttpRequest();   // Modern browser
      } 

      else
      {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP");  // Older browser 
      }

      xhhtp.onreadystatechange=function()
     {
     }
xhttp.open("POST""Customer.php"True);
xhttp.send();
}
</script>
</head>

The onreadystatechange Event

  • When a request to a server is sent, we want to perform some actions based on the response.
  • The onreadystatechange event is triggered every time the readyState changes.
  • The readyState property holds the status of the XMLHttpRequest.
  • Three important properties of the XMLHttpRequest object:


1. onreadystatechange
     It holds stores function
     xhhtp.onreadystatechange=function()
     {
     }
2.readyState
      xhttp.readystate  = it holds status of XMLHttpRequest Object
                                   0 to 4 States are available.
                                   0 means Request not initialized
                                   1 Sever connection established
                                   2 Received Request
                                   3 Request Processed
                                   4 Request Finished and Ready to Respond
3.status
     xhhtp.status   = it holds status request and response
                            200 Page Found
                            404 Page Not Found

Finally we know in onreadystatechange event,
when Request Finished and ready for respond and update page found then response will ready!
i.e
xhttp.readyState == 4 && xhttp.status == 200

<head>
<script>
function myfunction()
{
    var xhttp;
      if (window.XMLHttpRequest) 

      {
      xhttp = new XMLHttpRequest();   // Modern browser
      } 

      else
      {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP");  // Older browser 
      }

     xhhtp.onreadystatechange=function()
     {
     if (xhttp.readyState == 4 && xhttp.status == 200) {
            ---XML HTTP RESPONSE --- Next Section will see that
        }
    };
xhttp.open("POST""Customer.php"True);
xhttp.send();
}
</script>
</head>

Thank You!


No comments:

Post a Comment

What is AI- Artificial Intelligence - தமிழில்

Artificial Intelligence : Artificial intelligence leverages computers and machines to mimic the problem-solving and decision-making capabili...