zoukankan      html  css  js  c++  java
  • vue--http请求的封装--token

    export const FetchHandler = function (url,opt) {
    let paramStr = '';
    let token = '';
    for(key in opt){
    if (opt[key] !== ''){

    if (typeof opt[key] === 'object') {
    paramStr += key + '=' + JSON.stringify(opt[key]) + '&'
    } else {
    paramStr += key + '=' + opt[key] + '&'
    }
    }
    }
    paramStr = paramStr.substring(0,paramStr.length - 1)

    return new Promise((resolve,reject) => {
    AsyncStorage.getItem('token',(error,result)=>{
    token = result;
    }).then(result=>{
    fetch(url, {
    method: "POST",
    mode: "cors",
    headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    'token': token
    },
    body: paramStr
    })
    .then((response) => response.json())
    .then(function (response) {
    resolve(response)
    })
    .catch((reject)=>{
    reject('request error');
    })
    });

    })
    };
  • 相关阅读:
    vue父子组件传值的方式
    定时任务写法
    仅仅为笔记
    consul剔除某个服务
    mybatis批量查询
    一次eureka的事故
    feign的工作原理
    JVM优化
    threadlocal应用
    秋招总结
  • 原文地址:https://www.cnblogs.com/langqq/p/9087507.html
Copyright © 2011-2022 走看看