Vanilla Javascript版本的AJAX [英] Vanilla Javascript version of AJAX

查看:66
本文介绍了Vanilla Javascript版本的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我只想使用AJAX时,如何消除下载完整的jquery库的需要。是否有较小的文件专注于AJAX或此代码的Vanilla Javascript版本?

How can I remove the need to download a full jquery library when all I want to use is AJAX. Is there a smaller file that focuses on AJAX or is there a Vanilla Javascript version of this code?

<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){

            $.ajax({
                type: 'POST',
                url: 'cookies.php',
                success: function(data) {
                    alert(data);
                }
            });
   });
});
</script>


推荐答案

您可以尝试使用 XMLHttpRequest >如下所示。

You can try with XMLHttpRequest like below.

<!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

这篇关于Vanilla Javascript版本的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆