zoukankan      html  css  js  c++  java
  • 携带cookie进行数据请求

    前端进行数据请求有:普通的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'


  • 相关阅读:
    题解 P3071 【[USACO13JAN]座位Seating】
    [luogu]P3398 仓鼠找sugar
    快速输入输出
    Luogu P3939 数颜色
    HEOI2016/TJOI2016 排序
    POI2011 DYN-Dynamite
    USACO17JAN Promotion Counting
    AHOI2008 聚会
    Luogu P4907 A换B problem
    网络流24题 骑士共存问题
  • 原文地址:https://www.cnblogs.com/qiyecao/p/9760954.html
Copyright © 2011-2022 走看看