- 首先在react项目下的src文件夹中创建一个utils文件夹, 然后在utils文件夹中新建一个index.js文件
index.js中写入代码
const querystring = require("querystring");
export function httpGet (url) {
const result = fetch(url)
return result;
}
export function httpPost(url, params) {
const result = fetch(url, {
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded",
"Accept":"application/json,text/plain,*/*"
},
body: querystring.stringify(params)
})
return result
}
- 然后需要在src中又新建一个文件夹为api文件夹写入index .js和api.js这两个文件
2.1 index.js中的写法
import {
httpGet, httpPost } from '../utils/index'
import base from './base'
const api = {
getChengpin () {
return httpGet(base.ownUrl + base.chengpin)
},
getLogin(params) {
return httpPost(base.ownUrl + base.login, params)
}
}
export default api
2.2 base.js中的写法(配置文件)
const base = {
ownUrl: "http://iwenwiki.com",
chengpin: "/api/blueberrypai/getChengpinInfo.php",
login: '/api/blueberryapi/login.php'
}
export default base
本篇 结束