ajax请求体

jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等。

$.ajax({
    url:"http://www.test.com",    //请求的url地址
    dataType:"json",   //返回格式为json
    async:true,//请求是否异步,默认为异步,这也是ajax重要特性
    data:{"id":"1","name":"名字"},    //参数值
    timeout:'1000',//超时请求
    type:"GET",   //请求方式
    cache:'false',//是否缓存
    beforeSend:function(){
        //请求前的处理
    },
    success:function(req){
        //请求成功时处理
    },
    complete:function(){
        //请求完成的处理
    },
    error:function(){
        //请求出错处理
    }
});

请求成功失败函数可以这么写(注意函数不加参数!)
{
error: errorfunc,
success: getsuccess
}

 function getsuccess(res) {
        console.log(res)
                }
 function errorfunc(err) {
        console.log(err)
                }
原文地址:https://www.cnblogs.com/vinic-xxm/p/11707070.html