jq的 ajax

111 阅读1分钟

专门用于发送get请求的方法

$.get('',{},function(res){},’json’)

专门用于发送post请求的方法

$.post('',{},function(res){},’json’)

ajax:既可以发送get请求,也可以发送post请求

$.ajax({
    url:'http://www.bingjs.com:81/Grade/GetAll',    //设置请求地址
    type:'GET',    //设置请求类型(GET/POST)默认值是:GET
    data:{},       //设置请求参数数据
    dataType:'json',  //服务器返回的数据类型
    async:true,     //true表示异步请求(默认值),false表示同步请求
    beforeSend:function(xhr){
        //发送请求前的回调函数
        console.log(xhr);
        console.log('即将发送请求....');
    },
    success:function(data,textStatus,xhr){
        //请求成功后的回调函数
        console.log('请求成功');
        console.log(data);
        console.log(textStatus);
        console.log(xhr);
        data.forEach(r=>{
            let option = $('<option/>').html(r.GradeName).attr('value',r.GradeId)
            $('#grade').append(option)
        })
    },
    error:function(xhr,textStatus,errorThrown){
        //请求失败后的回调函数
        console.log('请求失败');
        console.log(xhr);
        console.log(textStatus);
        console.log(errorThrown);
    },
    complete:function(xhr,ts){
        //请求完成后的回调函数
        console.log(xhr);
        console.log(ts);
        console.log('请求已完成....');
    }
})