Jquery使用Ajax

文章描述:

jquery里面ajax使用的方法以及常用的知识

GET

$.ajax({
    type:"GET",
    url:" ",
    dataType:"json",
    success:function(res){
 
    }
});

POST

$.ajax({
    url:"",
    type:"post",
    dataType:"json",
    async: false,
    data:{id:"",name:""},
    success:function(data){
 
    },error:function(e){
        console.log(e.responseText);
    }
});

设置请求头header 参数

headers:{'Content-Type':'application/json;charset=utf8','token':'88888888'},
$.ajax({
    url:"",
    type:"post",
    dataType:"jsonp",
    headers:{'Content-Type':'application/json;charset=utf8','token':'88888888'},
    async: false,
    data:{id:"",name:""},
    success:function(data){
 
    },error:function(e){
        console.log(e.responseText);
    }
});

设置contentType: “application/json”,

在添加 contentType:“application/json“之后,向后台发送数据的格式必须为json字符串

data:"{'name':'宇智波斑','age':'20'}",

数据转化为字符串

var data = {
    uploadarray: uploadarray,
    messageInfo: {
        messageTitle: messageTitle,
        messageContent: messageContent,
        publisher: publisher
    },
    userId: userId
}
contentType: 'application/json',
data: JSON.stringify(data),
dataType: "json",

beforeSend函数使用

//没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据。这个就可以用beforeSend方法来实现
beforeSend:function(){
   This.html('正在登录中...');
},
success:function(data){
 
}
$.ajax({
    type:"GET",
    url:" ",
    dataType:"json",
    headers:{
        'Content-Type':'application/x-www-form-urlencoded;charset=utf8',
        'X-Access-Token':token,
        'currRoleCode':roleCode,
 
    },
    data:{'userId':user_id},
    success:function(res){
 
    }
});

 

 

发布时间:2021/07/19

发表评论