• react -搭建服务


    import 'whatwg-fetch';
    import 'es6-promise';
    require('es6-promise').polyfill();
    import * as common from 'common/common.js';

    function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
    return response.text();
    } else {
    common.toast("请求错误");
    common.closeLoading();
    var error = new Error(response.statusText);
    error.response = response;
    throw error;
    }
    }

    function parseJSON(response) {
    var result = JSON.parse(response);
    if(result.code == "99"){
    common.closeLoading();
    common.gotoLogin();
    }
    return result;
    }

    // 发送 get 请求
    export function get(url) {
    url = common.urlTransmit(url);
    var headers = {
    'Accept': 'application/json',
    'Content-Type' : 'application/json'
    }
    if(common.tvToken()){
    headers.tvToken = common.tvToken()
    }
    let requestConfig = {
    credentials: 'include',
    method: "GET",
    headers: headers
    }
    return fetch(url, requestConfig).then(checkStatus).then(parseJSON);
    }

    // 发送 post 请求
    export function post(url, paramsObj) {
    url = common.urlTransmit(url);
    var headers = {
    'Accept': 'application/json, text/plain, /',
    'Content-Type': 'application/x-www-form-urlencoded'
    }
    if(common.tvToken()){
    headers.tvToken = common.tvToken()
    }
    return fetch(url, {
    method: 'POST',
    credentials: 'include',
    headers: headers,
    body: obj2params(paramsObj)
    }).then(checkStatus).then(parseJSON);
    }

    // 将对象拼接成 key1=val1&key2=val2&key3=val3 的字符串形式
    function obj2params(obj) {
    var result = '';
    var item;
    for (item in obj) {
    result += '&' + item + '=' + encodeURIComponent(obj[item]);
    }

    if (result) {
        result = result.slice(1);
    }
    
    return result;
    

    }

    最难就是坚持
  • 相关阅读:
    原子核壳模型程序 BigStick 的用法
    c++ 中的下三角阵矩阵元标记
    BCS方程和Bogoliubov变换
    圆膜振动问题
    核结构单体跃迁算符
    python画球谐函数
    gnuplot 绘制球谐函数图
    shell 脚本小知识集锦
    6.12学习总结
    PHP网上商城
  • 原文地址:https://www.cnblogs.com/pp-air/p/12084079.html
Copyright © 2020-2023  润新知