AJAX = Asynchronous JavaScript And XML. AJAX just uses a combination of:
A browser built-in XMLHttpRequest object(to request data from a web server)
JavaScript and HTML DOM(to display or use data)
AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it’s possible to update parts of a web page, without reloading the whole page.
<!DOCTYPE html> <html> <body> <h2>The XMLHttpRequest Object</h2> <divid="demo"> <p>Let AJAX change this text.</p> <buttontype="button"onclick="loadDoc()">Change Content</button> </div> <script> // Create an XMLHttpRequest object if (window.XMLHttpRequest) { //For all modern browsers(Chrome, Firefox, IE, Edge, Safari, Opera) xmlhttp = newXMLHttpRequest(); } else { //For the old version IE browser xmlhttp = newActiveXObject("Microsoft.XMLHTTP"); } // Send a request // xhttp.open(method, url, async) xhttp.open("post", "http://localhost:8080"); xhttp.send(); //Process data xhttp.onreadystatechange = function (resp) { // When readyState is 4 and status is 200, // the response is ready if (this.readyState == 4 && this.status == 200) { // } }; </script> </body> </html>
jQuery AJAX
jQuery load() Method
THe jQuery load() method loads data from a server and puts the returned data into the selected element[$(selector)] $(selector).load(url, data, callback)
The required URL parameter specifies the URL you wish to load.
The optional DATA parameter specifies a set of query key/value pairs to sent along with the request.
The optional CALLBACK parameter is the name of a function to be executed after the load() method is completed.
<!DOCTYPE html> <html> <head> <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function () { //An event occurs $("button").click(function () { $("#div1").load("demo_test.txt", function (responseTxt, statusTxt, xhr) { //responseText -contains the resulting content if the call succeeds. //statusTxt -contains the status of the call //xhr -contains the XMLHttpRequest object if (statusTxt == "success") alert("External content loaded successfully!"); if (statusTxt == "error") alert("Error: " + xhr.status + ": " + xhr.statusText); }); }); }); </script> </head> <body> <divid="div1"><h2>Let jQuery AJAX Change This Text</h2></div> <button>Get External Content</button> </body> </html>
jQuery $.get() method
The $.get() method request data from the server with an HTTP GET request. $.get(URL,callback)
The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<!DOCTYPE html> <html> <head> <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function () { $("button").click(function () { $.get("demo_test.asp", function (data, status) { alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> </head> <body> <button>Send an HTTP GET request to a page and get the result back</button> </body> </html>
jQuery $.post() method
The $.post() method requests data from the server using an HTTP POST request. $.post(URL, data, callback)
The required URL parameter specifies the URL you wish to request.
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
axios({ method: 'get', url: 'http://localhost:8080', }).then(function (response) { <!-- process response data --> resp.data });
Copyright:
Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.