当我只想使用AJAX时,如何消除下载完整的jquery库的需要。是否有一个较小的文件专注于AJAX,还是此代码的Vanilla Javascript版本?
<script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $.ajax({ type: 'POST', url: 'cookies.php', success: function(data) { alert(data); } }); }); }); </script>
您可以尝试使用 XMLHttpRequest, 如下所示。
<!DOCTYPE html> <html> <body> <h2>The XMLHttpRequest Object</h2> <button type="button" onclick="loadDoc()">Request data</button> <p id="demo"></p> <script> function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("POST", "cookies.php", true); xhttp.send(); } </script> </body> </html>
演示: https : //www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_first
参考: https : //www.w3schools.com/js/js_ajax_http_send.asp