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});
  • 相关阅读:
    [整理]修改git 默认编辑器为vim
    [转]如何清空Chrome缓存和Cookie
    [整理]docker内部时区修改的两种方法
    [译]10个有关SCP的命令
    [译]在python中如何有效的比较两个无序的列表是否包含完全同样的元素(不是set)?
    通过设计表快速了解sql语句中字段的含义
    [整理]什么是排序算法的稳定性,为什么它很重要?
    pyinstaller打包自己的python程序
    [问题解决]ps aux中command命令相同,如何找出自己要的进程号?
    [常识]Windows系统里休眠和睡眠的区别?
  • 原文地址:https://www.cnblogs.com/yayaxuping/p/9585637.html
Copyright © 2011-2022 走看看