zoukankan      html  css  js  c++  java
  • fetch的总结

    // 通过fetch获取内容
    /**
    * Headers=='application/json'
    *
    */
    fetch('/marketStrategy/getTagNodeTree?id=' + treeNode.props.eventKey,
    {
    // 在URL中写上传递的参数
    method: 'GET',
    headers: new Headers({
    'Accept': 'application/json' // 通过头指定,获取的数据类型是JSON
    })
    })
    .then((res) => {
    return res.json();
    })
    .then((res) => {
    // var list = res.list;
    // let parse = JSON.parse(res);
    // console.log('resresres============' + JSON.stringify(parse.list));
    treeNode.props.dataRef.children = res.list;
    this.setState({
    treeData: [...this.state.treeData],
    });
    })
    // post请求的列表
    // 请求成功了status ==200----300做一个判断,不然400---500也会返回到这里
    // fetch不支持jsonp

    fetch(
    '/marketStrategy/list',
    {
    method: 'POST',
    mode: "no-cors",
    headers: new Headers({
    'Accept': 'application/json', // 通过头指定,获取的数据类型是JSON
    'Content-Type': 'application/x-www-form-urlencoded'
    }),
    credentials: 'include', // 强制加入凭据头
    body: new URLSearchParams([]).toString()
    })
    .then((Response) => {
    if (Response.status >= 200 && Response.status < 300) {
    return Response.json();
    } else {
    return { code: Response.status }
    }
    })
    .then((res) => {
    console.log(res)
    })

    //对于fetch的封装
    var Event = {
    Post: function (url, options) {
    var that = this;
    that.commonHttp(url, options, 'POST');
    },
    Get: function (url, options) {
    var that = this;
    that.commonHttp(url, options, 'GET');
    },
    commonHttp: function (url, options, type) {
    var Obj = {
    method: type,
    headers: new Headers({
    'Accept': 'application/json' // 通过头指定,获取的数据类型是JSON
    }),
    credentials: 'include',
    };
    if (type === 'GET') {
    var urlParame = '';
    for (var i in options) {
    urlParame += options[i] + '&';
    }
    url += '?' + urlParame;
    }
    if (type === 'POST') {
    Obj.headers = new Headers({
    'Accept': 'application/json', // 通过头指定,获取的数据类型是JSON
    'Content-Type': 'application/x-www-form-urlencoded'
    })
    var arr = [];
    for (let item in options) {
    arr.push([item, options[item]]);
    }
    Obj.body = new URLSearchParams(arr).toString();
    }
    fetch(url, Obj).then((res) => {
    if (res.status >= 200 && res.status < 300) {
    return res.json;
    }
    }).then((res) => {
    return res;
    })
    }
    }
    Event.Post('utrl', { a: 1 });
    Event.Get('url',{b:1});
  • 相关阅读:
    delphi 的插件机制与自动更新
    delphi 的 ORM 框架
    canner CMS 系统 (公司在台湾) https://www.canner.io/
    区块链 ---- 数字货币交易
    BIM平台 http://gzcd.bim001.cn
    TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
    UniGUI 如何进行 UniDBGrid 的单元 Cell 的计算 ?
    国产 WEB UI 框架 (收费)-- Quick UI,Mini UI
    iOS尽量不要在viewWillDisappear:方法中移除通知
    Tips:取消UICollectionView的隐式动画
  • 原文地址:https://www.cnblogs.com/yayaxuping/p/9585637.html
Copyright © 2011-2022 走看看