# wx-ajax

> 微信小程序 wx.request 二次封装支持Promise 和 callback 回调函数

Latest version **1.1.0** (published 2019-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install wx-ajax
pnpm add wx-ajax
yarn add wx-ajax
bun add wx-ajax
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-08-24 |
| First published | 2019-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Mason |
| Maintainers | mason_li |
| Keywords | 微信小程序, wx.request, axios, promise, ajax, fetch, wechat, xhr, http, weixin-ajax, wechat-axios |

## Links

- npm: https://www.npmjs.com/package/wx-ajax
- Repository: https://github.com/displayLi/wx-ajax
- Homepage: https://github.com/displayLi/wx-ajax#readme
- Issues: https://github.com/displayLi/wx-ajax/issues
- npm.io page: https://npm.io/package/wx-ajax

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-08-24
- 1.0.9 — 2019-08-24
- 1.0.8 — 2019-08-24
- 1.0.7 — 2019-08-24
- 1.0.6 — 2019-08-18
- 1.0.5 — 2019-08-18
- 1.0.4 — 2019-08-18
- 1.0.3 — 2019-08-18
- 1.0.2 — 2019-08-17
- 1.0.1 — 2019-08-17
- 1.0.0 — 2019-08-17

## README

# wx-ajax

<!-- logo -->
<div align=center>
    <img height="200" src="https://raw.githubusercontent.com/displayLi/wx-ajax/master/logo.png"/>
</div>

## 特性

1、基于 wx.request 的二次封装支持 Promise 和原有 callback 回调函数 （上传文件模块开发中）

2、使用门槛低，容易上手，提供和axios相似体验，兼容低版本手机系统。

![npm (scoped)](https://img.shields.io/badge/npm-v5.6.0-brightgreen.svg) 
![installsize (scoped)](https://img.shields.io/badge/install%20size-74.2kb-yellow.svg) 
![build (scoped)](https://img.shields.io/badge/build-passing-yellowgreen.svg) 
![licenses (scoped)](https://img.shields.io/badge/licenses-MIT-blue.svg) 

## 安装

```
npm i wx-ajax -S --production
```

## 使用方法

<a href="https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html?search-key=npm">小程序如何使用 npm 模块</a>

***参数说明***


|字段|类型|默认值|必填|
|:---|:---:|---:|---:|
|path|String|无|是|
|method|String|GET|GET请求可省略|
|showLoading|Object|false 不显示, true显示默认的配置, 默认值是false|否|
|header|Object|{content-type:application/json, accessToken: wx.getStorageSync("token")}|否|
|dataType|String|json|否|
|data|Object|{}|否|
|success|Function|null|Promise下省略|
|fail|Function|null|Promise下省略|


```
// showLoading 有三个字段
showLoading：{
    msg:"可以自定义你的loading提示",
    endTime:200  // loading的延时关闭 默认 500毫秒
    mask: false  // 默认是false 和微信小程序配置的一样
}

```


***引入***

***1、require***
```
const api = require('wx-ajax')(host);  // 你的服务器域名

// 举例:
const api = require('wx-ajax')('http://jsonplaceholder.typicode.com');  // 实例化的ajax对象

```

***2、es6***

```
import api from 'wx-ajax';
const api = api(host);   // 你的服务器域名

// 举例：
const api2 = api('http://api.link97.com:8081');
```
#### Promise 请求示例
***1、POST***

```
api.ajax({
    path:'/posts',
    method:'POST',
    showLoading:true,
    data:{
        id:1
    }
}).then(({ data }) => console.log(data));
```

***2、GET***

```
api.ajax({
    path:'/posts',
    showLoading:true
    data:{  // get的传参方式跟post是一样的 都是data对象传参 当然也可以在url处拼接
        id:1
    }
}).then(({ data }) => console.log(data));
```

***3、PUT***

```
api.ajax({
    path:`/posts/${editId}`,
    showLoading:{
        endTime:600,
        msg:"修改中..."
    },
    data:{ 修改的data }
}).then(res => console.log(res));
```


***4、DELETE***

```
api.ajax({
    path:`/posts/${deleteId}`,
    showLoading:{
        endTime:600,
        msg:"删除中..."
    }
}).then(res => console.log(res));
```

#### 回调函数请求示例

***1、POST***

```
api2.ajax({
    path:'/goods',
    method:'POST',
    showLoading:true,
    data:{
        id:1
    },
    success(res) {
        console.log(res);
    },
    fail(err) {
        console.log(err);
    }
})
```

***2、GET***

```
api2.ajax({
    path:'/goods',
    showLoading:true
    data:{  // get的传参方式跟post是一样的 都是data对象传参 当然也可以在url处拼接
        id:1
    },
    success(res) {
        console.log(res);
    },
    fail(err) {
        console.log(err);
    }
})
```

***3、PUT***

```
api2.ajax({
    path:`/goods/${editId}`,
    showLoading:{
        endTime:600,
        msg:"修改中..."
    },
    data:{ 修改的data },
    success(res) {
        console.log(res);
    },
    fail(err) {
        console.log(err);
    }
})
```


***4、DELETE***

```
api2.ajax({
    path:`/goods/${deleteId}`,
    showLoading:{
        endTime:600,
        msg:"删除中..."
    },
    success(res) {
        console.log(res);
    },
    fail(err) {
        console.log(err);
    }
})

```
***同样支持axios 方法 方法内接受三个参数 path：String， data：Object，header：Object (header、data选填，path必填)***
```
api.ajax('/posts').then(res => console.log(res)).catch(err => console.log(err));

```

Copy right Mason 有问题联系 QQ：463961434

Author By Mason @ LINK + 创意工作室

---
_Source: https://npm.io/package/wx-ajax · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
