ajax

原生js的ajax

var btn=document.getElementById('btn');
btn.onclick =function(){
var xhr;
        if(window.XMLHttpRequest){
            xhr=new XMLHttpRequest();
        }else{
            xhr= new ActiveXObject('microsoft.XMLHTTP');
        }
    console.log(xhr);
    //第二步发送请求
   //xhr.open('get','ajaxRode.html',true);//get方式
    xhr.open('post','ajaxRode.html',true);//post方式
    xhr.setRequestHeader ('Content-type','application/x-www-form-urlencoded');
    //第三步告诉要什么
    xhr.send();
    //第四步得到返回结果
    xhr.onreadystatechange=function(){
        if(arr.readyState==4){
            if(arr.status==200){
               console.log(arr.responseText);
            }
        }
    }

}
</script>

  jquery的ajax方法

$.ajax({
    url:'/comm/test1.php',
    type:'POST', //GET
    async:true,    //或false,是否异步
    data:{
        name:'yang',age:25
    },
    timeout:5000,    //超时时间
    dataType:'json',    //返回的数据格式:json/xml/html/script/jsonp/text
    beforeSend:function(xhr){
        console.log(xhr)
        console.log('发送前')
    },
    success:function(data,textStatus,jqXHR){
        console.log(data)
        console.log(textStatus)
        console.log(jqXHR)
    },
    error:function(xhr,textStatus){
        console.log('错误')
        console.log(xhr)
        console.log(textStatus)
    },
    complete:function(){
        console.log('结束')
    }
})

  

posted @ 2017-03-23 13:58  豆豆豆芽儿  阅读(114)  评论(0)    收藏  举报