wiki/开发/前端/AJAX手册.md
2025-01-02 10:46:09 +08:00

60 lines
883 B
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.

## 定义
AJAX使用XMLHttpRequest对象与服务器异步通信可以使用JSONXMLHTMLtext文本等格式发送和接收数据
## URL
```
http://lzcwy.cn/api/user?name=lzc&age=18
协议://域名/资源地址?查询参数1&查询参数2
```
## 使用axios传递查询参数
```
axios({
url: 'http://域名/资源路径'
params: {
key1: 'value1',
key2: 'value2'
}
}).then (res => {
//接受并使用数据
})
```
## 使用axios提交数据
```
axios({
url: 'url',
method: 'post',
data: {
key: 'value'
},
})
```
## 错误处理
```
axios({
//请求选项
}).then(res => {
//处理数据
}).catch(err => {
//处理错误
})
```
## 快速收集表单元素的值
```
form-serialize
const form = document.qs()
const data = serialize(form, {hash: true,empty:true})
生成的数据格式-true 为js
empty 是否收集空数据
```