前端学习---ajax


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>ajax</title>
    <script>

        //AJAX 
        //原生ajax
        // XMLHttpRequest XHR  ,   ActiveXObiect(ie5 ie6):都是对象
        //1、创建XHR对象 2、连接服务器 3、发送请求  4、接受数据
        function getDate(){
         
         var xhr;
         if(window.XMLHttpRequesr){
            xhr= new XMLHttpRequest();
         }else{
             xhr = new ActiveXObject("Microsoft,XMLHttp");
         };
         xhr.open('get','url',true); //请求类型(get.post.put)访问的路径、异步:true 异步,false:同步
         xhr.setReauestHeader("Content-Type","application/json");//如此设置,表示请求想要得到的数据是json格式
         xhr.send(JSON.stringify({name:'sonia'}));//post 请求传入string
         xhr.onreadystatechange = function(){
             if(xhr.readyState == 4 && xhr.readyState == 200)//
             {
                 console.log(xhr.XMLHttpRequest);//响应的数据
             }
         }
        }
        //jquery中的ajax
        $.ajax({
            type:'get',//请求类型
            url:'',//请求路径
            async:true,//异步
            data:data,//请求是传入的数据
            dataType:'jsonp',//jsonp:可跨域,json:正常
            success:function(){//请求成功时

            },
            error:function(){//请求失败时

            }
        });
    </script>
</head>
<body>
    
</body>
</html>

猜你喜欢

转载自blog.csdn.net/zhujiarunm/article/details/82155677