Files
mes-ui-d2/docs/zh/sys-ajax/README.md
liyang 04470e69ab 文档更新
Former-commit-id: 1285570867011f86a3991c3ae598192951cc3f2f [formerly 1285570867011f86a3991c3ae598192951cc3f2f [formerly 1285570867011f86a3991c3ae598192951cc3f2f [formerly 1285570867011f86a3991c3ae598192951cc3f2f [formerly 293f784fe509a79f276015d3647f954340343066 [formerly 9f69c12174116db35ca32bad96ef5ba11de35397]]]]]
Former-commit-id: 704d15270edf4feeff893ad9b5cc25ae201a093d
Former-commit-id: 81ee93da041be7b177ecd7b327de9cfadb2969f8
Former-commit-id: bd5fe14e21a1385f2c5b309d248442eac481c989 [formerly 727cb0124ab4db83b0c485ba717c49ee2d7dfaf9]
Former-commit-id: 44bb97adb6f9a5ee0f0b298752e8c74a45301c10
Former-commit-id: 64239434f1ef1dd8812ff166ae5d5306ee3e601e
Former-commit-id: 01618dcab298c103dc57748e6ad72ad81af7bb4b
Former-commit-id: 7e4babf533d723455d8b8bca87cc4b2c6c79cbe3
Former-commit-id: 54cf9b9949027aa08465c85960fe1853cc209c50
2018-08-27 13:37:02 +08:00

153 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
sidebar: auto
---
# 异步请求
D2Admin 使用 [axios](https://github.com/axios/axios) 作为异步请求工具,并做了一些封装。
| axios | 地址 |
| --- | --- |
| Github | [https://github.com/axios/axios](https://github.com/axios/axios) |
| npm | [https://www.npmjs.com/package/axios](https://www.npmjs.com/package/axios) |
| 中文文档 | [https://www.kancloud.cn/yunye/axios/234845](https://www.kancloud.cn/yunye/axios/234845) |
## 介绍
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
* 支持浏览器和node.js
* 支持promise
* 能拦截请求和响应
* 能转换请求和响应数据
* 能取消请求
* 自动转换JSON数据
* 浏览器端支持防止CSRF(跨站请求伪造)
## 浏览器支持
![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/src/safari/safari_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Edge](https://raw.github.com/alrra/browser-logos/master/src/edge/edge_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png) |
--- | --- | --- | --- | --- | --- |
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
[![Browser Matrix](https://saucelabs.com/open_sauce/build_matrix/axios.svg)](https://saucelabs.com/u/axios)
## 使用方式
axios 默认的使用方式在这里不做介绍D2Admin 推荐在您的项目中使用下面的方式获取数据:
### 修改 baseURL
默认的请求地址在 `d2-admin/.env`
```
VUE_APP_API=/api/
```
上述设置将在您访问 `/demo/a` 时实际去访问 `/api/demo/a`
如果您希望不同的环境使用不同的请求地址,可以在 `d2-admin/.env.development` 中添加设置(示例):
```
VUE_APP_API=/api-dev/
```
这样您在开发环境和正式环境就有了不同的公共请求地址,在开发环境访问 `/demo/a` 时实际去访问 `/api-dev/demo/a`
### 通用配置
您在开始使用 D2Admin 开发您的项目之前,应该首先修改 `d2-admin/src/plugin/axios/index.js` 下的设置。
默认的设置需要遵循下面的数据返回格式约定:
``` js
{
// 和后台约定的状态码
code: 0,
// 后台返回请求状态信息
msg: '返回信息',
// data 内才是真正的返回数据
data: {
list: [
...
]
}
}
```
当发生错误(注:这里指的错误不是 http 错误,而是和后台约定好的错误类型)时返回的数据示例:
``` js
{
// 和后台约定的状态码
code: 'unlogin',
// 后台返回请求状态信息
msg: '用户没有登录'
}
```
如果针对某个错误(注:这里指的错误不是 http 错误,而是和后台约定好的错误类型)指定处理方法,应该在响应拦截器中加入对应的代码:
``` js
service.interceptors.response.use(
response => {
// 成功返回数据,在这里判断和后台约定的状态标识
},
error => {
// 发生 http 错误,在这里判断状态码
}
)
```
如果需要针对某个 http 错误指定处理方法,应该在响应拦截器中第二个参数中添加对应的代码。
::: tip
在默认的设置中,如果您的接口没有返回 code 字段,将不会进行状态(非 http 状态,而是和后台约定好的状态类型)判断,直接返回 axios 请求返回的数据。
:::
### 设计 API
假设您有一个返回数据的 API 接口,想访问它,您首先应该在 `d2-admin/src/api` 文件夹内创建合适的文件目录,例如:`d2-admin/src/api/demo/business/table/1/index.js`,这个文件中应该导出一个或者多个请求:
``` js
import request from '@/plugin/axios'
export function BusinessTable1List (data) {
return request({
url: '/demo/business/table/1',
method: 'post',
data
})
}
```
### 使用 API 获取数据
在上面的步骤中创建了 API 文件,您应该在页面中这样使用:
``` vue
<script>
import { BusinessTable1List } from '@/api/demo/business/table/1'
export default {
methods: {
handleSubmit (form) {
BusinessTable1List({
name: ''
})
.then(res => {
// 返回数据
})
.catch(err => {
// 异常情况
})
}
}
}
</script>
```
而不是在页面中直接调用 axios。
::: tip
虽然没有强制规定,请注意您的 API 文件夹结构规律性
:::