get&&post请求
Posted bree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了get&&post请求相关的知识,希望对你有一定的参考价值。
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请求的主要内容,如果未能解决你的问题,请参考以下文章