前端進行數據請求有:普通的ajax(json)請求,jsop跨域請求,cors跨域請求,fetch請求...PC端這些請求方式中,普通的ajax(json)請求和jsop跨域請求是默認攜帶cookie的,而cors跨域請求和fetch請求默認是不攜帶cookie的。因此,當我們的請求需要攜帶cookie時,我們就要對cors跨域請求和fetch請求這兩中請求方式進行特殊配置處理。對於做移動端的童鞋來說,要是能把項目運行在PC端中最好不過,對於調試過程中的BUG一目了然,所以做特殊處理后更有利於我們在PC端進行調試。
fetch請求方式:
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'