Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AJAX (short for <code>Asynchronous Javascript And XML</code> ) is a javascript object through which you can send requests to the webserver and get back its response asynchronously. Most of the modern browser support initializing AJAX requests via</p> <pre><code>var xmlHttp = new XMLHttpRequest(); </code></pre> <p>Here is how you send a request</p> <pre><code>xmlHttp.open('GET' , 'file_in_server.php', false); </code></pre> <p>'GET' may be any other type of requests (like 'POST', 'PUT'...) and 'file_in_server.php' is the file where you send request, and <code>false</code>means you are doing it synchronously. </p> <p>After that line, you can write:</p> <pre><code>xmlHttp.send(); </code></pre> <p>which sends the request you <code>open()</code>ed previously.</p> <p>Now, the server sends back the response which you can capture by</p> <pre><code>var response = xmlHttp.responseText </code></pre> <p>Now response is anything echoed by your <code>file_in_server.php</code> </p> <p>You can make use of asynchrous nature of AJAX. here's the sample code.</p> <pre><code>var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 &amp;&amp; xmlHttp.status == 200){ var capturedText = xmlHttp.responseText; } }; xmlHttp.open('GET','file.php?a=1&amp;b=2', true); //sends GET requests, with querystring. xmlHttp.send(); </code></pre> <p>The above code is asynchronous in nature. It does not block other script execution. The first line creates the XMLHttpRequest object On the second line, we are just creating an event listner that says that when the ready state of xmlHttp object is changes the function should be executed. . Now the execution goes to <code>xmlHttp.open</code> which initializes the process of requesting the server. This statement makes the XMLHttpRequest know that the request is asynchronous. after that the request is sent. As soon as the request is sent, the readystate of the object is changed, and the function above is called. That function checks for two condition, readystate of the object and returned status code and if the readystate is 4 (which means, ready), and the staus is 200 (which means OK), then it catches the response in the variable. Now you can use that variable to fulfill your motive.</p> <p>if your file.php echoes the username currently logged in, then that will be catched in the variable capturedText</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload