前端进行数据请求有:普通的ajax(json)请求,jsop跨域请求,cors跨域请求,fetch请求...PC端这些请求方式中,普通的ajax(json)请求和jsop跨域请求是默认携带cookie的,而cors跨域请求和fetch请求默认是不携带cookie的。所以,当咱们的请求须要携带cookie时,咱们就要对cors跨域请求和fetch请求这两中请求方式进行特殊配置处理。对于作移动端的童鞋来讲,要是能把项目运行在PC端中最好不过,对于调试过程当中的BUG一目了然,因此作特殊处理后更有利于咱们在PC端进行调试。前端
fetch('/community/getCommunityActivityByCommunityId', {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
credentials: 'include',
body:"communityId="+this.props.location.query.communityId
})
.then((res) => { return res.json(); })
.then((data) => {
//请求成功
})
.catch((e) => {
//报错
});
咱们要在请求头中添加上这个配置:
credentials: 'include'
$.ajax({
type: "post",
url:"/activity/queryPrizeRecord",
dataType: "json",
xhrFields: {
withCredentials: true
},
crossDomain: true,
data:{},
success:function(data){
},
error:function(e){
}
})
xhrFields: {
withCredentials: true
},
crossDomain: true