JS XMLHttpRequest

XMLHttpRequest

 http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html

 

老版本  Level 1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
          var http=new XMLHttpRequest();
            http.open('get','http://localhost:60479/weatherforecast',true)
            http.send()
            http.onreadystatechange=function(){
                    if ( http.readyState == 4 && http.status == 200 ) {
                          console.log( http.responseText );
                        } else {
                            console.log( http.statusText );
                        }
            }
    </script>
</body>
</html>

 

 

    <button onclick="f()">btn</button>
    <script>
        function f()
        {
            //创建一个对象
            let xhr = new XMLHttpRequest();
            //参数1请求方式,参数2可以是相对地址,也可是绝对地址。参数3是否异步
            xhr.open("get", "weatherforecast", false);
            xhr.send();
            alert(xhr.responseText)
        }
    </script>

 

posted @ 2017-07-30 17:30  富坚老贼  阅读(218)  评论(0编辑  收藏  举报