Performing AJAX Calls in JavaScript
While jQuery simplifies AJAX operations, it is possible to make AJAX calls using plain JavaScript. Here's how:
Vanilla JavaScript:
function loadXMLDoc() { const xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === XMLHttpRequest.DONE) { if (xmlhttp.status === 200) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } else if (xmlhttp.status === 400) { alert('There was an error 400'); } else { alert('Something else other than 200 was returned'); } } }; xmlhttp.open("GET", "ajax_info.txt", true); xmlhttp.send(); }
jQuery:
$.ajax({ url: "test.html", context: document.body, success: function() { $(this).addClass("done"); } });
By utilizing the vanilla JavaScript method, you can make AJAX calls directly, providing flexibility and allowing for manipulation of the response without relying on jQuery.
The above is the detailed content of How Can I Make AJAX Calls Using Plain JavaScript and jQuery?. For more information, please follow other related articles on the PHP Chinese website!