https://github.com/cytle/ma-request
小程序请求,返回一个promise.
https://github.com/cytle/ma-request
Last synced: 3 months ago
JSON representation
小程序请求,返回一个promise.
- Host: GitHub
- URL: https://github.com/cytle/ma-request
- Owner: cytle
- Created: 2018-01-14T06:25:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-16T01:00:50.000Z (over 1 year ago)
- Last Synced: 2025-06-13T07:04:48.988Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 238 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ma-request
小程序请求,返回一个`promise`.
## Usage
```js
import request from 'ma-request';
// 基本用法仅为示例,并非真实的接口地址
request('test.php', {
method: 'POST', // default GET
data: {
x: '',
y: ''
},
headers: {
'User-Agent': 'Request-Promise'
},
})
.then((data) => {
console.log(data);
}, (res) => {
console.log(res);
});
```
### request
```js
async function demo() {
const res1 = await request.get({
url: 'test.php',
});
const res2 = await request.get('test.php'); // 等同上面
// 除get外还有options, head, post, put, delete, trace, connect
const res3 = await request.post('test.php', {
data: { x: '' },
});
}
```
### setGlobalHooks(hooks) 设置全局钩子
```js
setGlobalHooks({
before(options) {
// 拦截请求,最后需要返回一个promise
},
success(res, options) {
// 请求成功,可以处理结果,最后需要返回一个promise
},
fail(res, options) {},
});
```