Fork me on GitHub

交互ajax

原生的js
封装ajax
1.创建ajax对象
var oAjax=new XMLHttpRequest();//不兼容IE6
var oAjax=new ActiveXobject('Microsoft.XMLHTTP');//iE6
2.发送请求
第一种
oAjax.open("GET",url+"?"+data(),true)
oAjax.send();//发送数据
第二种
oAjax.open("POST",url,true)
oAjax.setRequestHeader("Content-type",'appLication/x=www=form-urlencoded');
oAjax.send(data);//发送数据
3.监听
oAjax.onreadystatechange=function(){
    if(oAjax.readyState==4){
        if(oAjax.status>=200&&oAjax.status<300){
            succ&&succ(oAjax.responseText);
        }else{
            error&&error(oAjax.status());
        }
    }
};
注意:data()处理数据 主要是将数据变成json格式
原生的跨域
利用script标签或者利用代理

jquery
1.同一域中
$.ajax({
    type:"get",//可以使get或者是post
    async:"true",//默认是true表示的是异步的
    url:"xxx.php",
    data:"xxxx",
    success:function(result){
        alert(result);
    },
})
2.跨域
$.ajax({
    type:"get",
    url:"xxx",
    dataType:"jsonp",
    jsonp:"jsoncallback",
    success:function(data){
        alert(data);
    },
    error:function(){
        alert(fail);
    },
})

posted @ 2016-08-10 14:19  zhang_yx  阅读(112)  评论(0编辑  收藏  举报