ajax-原理及应用

<body>
    <input type="button" value="请求" onclick="req();">
</body>
<script type="text/javascript">
    function req() {
        // xml 一个对象
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(){
            //console.log(xhr);
            if(this.readyState == 4){
                alert("请求结束");
                alert('内容是:'+this.responseText);
            }
        }
        xhr.open('get', 'ajaxed', true);
        xhr.send(null);
    }
</script>

总结:
ajax请求的主要流程:

  • new一个 XMLHttpRequest对象;
  • 监听浏览器请求状态onreadystatechange。整个请求分为四步,readyState的状态值分别为:1234。当请求为4的时候,标志请求完成;
  • responseText为请求的返回结果;
  • open(’'请求方式, ‘请求url’, true);
  • send() 请求发送。

猜你喜欢

转载自blog.csdn.net/Wake_me_Up123/article/details/83899544
今日推荐