码迷,mamicode.com
首页 > 其他好文 > 详细

get&&post请求

时间:2019-12-07 23:10:06      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:str   fetch   slice   color   for   text   hat   gif   encode   

get&&post请求

request.js文件

import { fetch as fetchPro } from "whatwg-fetch";
import qs from "qs";


const get = (options) => {
    let url = options.url;
    let data = options.data;

    if (data) {
        var str = "";
        for (var key in data) {
            str += "&" + key + "=" + data[key];
        }

        url = url + "?" + str.slice(1);
    }



    var result = fetchPro(url, {
        headers: {
            "content-type": "application/json",
            ...options.headers
        }
    }).then(res => res.json());


    return result;
}


const post = (options) => {
    var result = fetchPro(options.url, {
        method: options.method,
        body: qs.stringify(options.data),
        headers: {
            "content-type": "application/x-www-form-urlencoded"
        }
    }).then(res => res.json())

    return result;
}


export default {
    get,
    post
}

 

get&&post请求

标签:str   fetch   slice   color   for   text   hat   gif   encode   

原文地址:https://www.cnblogs.com/Bree/p/12003760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!