ajax封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
axios({
url:'http://localhost:8082/depart/findAll'
}).then(res=>{
console.log(res);
})
</script>
</html>
使用axios.get发送请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
axios.get('http://localhost:8082/depart/findByDname',
{params:{Dname:'学生部'}})
.then(res=>{
console.log(res);
}).catch(err=>{
console.log(err)
})
</script>
</html>
axios全局配置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
axios.defaults.baseURL = 'http://localhost:8082/depart';
axios.defaults.timeout=5000;
axios.get('/findByDname',
{params:{Dname:"学生部"}})
.then(res=>{
console.log(res);
}).catch(err=>{
console.log(err)
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<style>
.show{
padding-left: 30px;
padding-top: 30px;
position: absolute;
top:40px;
left:50px;
width: 500px;
height: 300px;
background: #f1f1f1;
}
</style>
</head>
<body>
<div class="show">
<button onclick="testGet()">GET请求</button>
<button onclick="testPost()">POST请求</button>
<button onclick="testPut()">PUT请求</button>
<button onclick="testDelete()">DELETE请求</button>
</div>
</body>
<script>
//创建实例
let newVar = axios.create({
baseURL:'http://localhost:8082/depart',
timeout:5000
})
newVar({
url:'/findAll'
}).then(res=>{
console.log(res)
})
</script>
</html>
在vue模块的封装
import axios from "axios";
export function request(config) {
axios({
url:config
}).then(res=>{
console.log(res)
})
}
import {request} from "./network/request/requset";
request('http://localhost:8082/depart/findAll')
post
常用的编码类型有这几种:1.application/x-www-form-urlencoded(常用于原生form表单提交);2.multipart/form-data(常用于文件传输);3.application/json(常用JSON格式编码)
注意:不同的Content-type需要对应不同的参数格式
1.application/x-www-form-urlencoded。参数格式:需要将正常的对象转化
例如:var data={
name:'小明',
age:'18'
}
data=qs.stringify(data)
注意: qs.stringify将对象序列化。使用qs.stringify函数时需要引入qs(通过npm安装)
2.multipart/form-data。参数格式:form-data
例如:var data=new FormData();
data.append("name","小明");
data.append("age","18");
3.application/json。参数格式:JSON对象
例如:var data={
"name","小明",
"age","18"
}
res.data表示的是后端向前端传输的对象obj
res.data.data表示的是后端向前端传输的对象obj属性名称为data对应的属性值
1、Content-Type: application/json
test () {
let temp = {
'userName': '楠晗',
'pwd': '123456'
}
// temp对应的是请求体的内容,必须是对象,不能是变量
this.$axios.post('http://localhost:8888/book/query', temp, {
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
.then(res => {
console.log(res.data.data)
}).catch(err => {
console.log(err)
})
}
}
后端:
@PostMapping("/query")
public RestResponse queryTest(@RequestBody User user){
System.out.println(user);
return RestResponse.ok("已收到");
}
2、Content-Type: multipart/form-data
test () {
let temp = new FormData()
temp.append('userName', 'hangover')
temp.append('pwd', '123456')
this.$axios.post('http://localhost:8888/book/query', temp)
.then(res => {
console.log(res.data.data)
}).catch(err => {
console.log(err)
})
}
@PostMapping("/query")
public RestResponse queryTest(User user){
System.out.println(user);
return RestResponse.ok("已收到");
}
3、Content-Type: application/x-www-form-urlencoded
npm install qs
<script>
import qs from 'qs'
test () {
let temp = {
'userName': '楠晗',
'pwd': '123456'
}
this.$axios.post('http://localhost:8888/book/query', qs.stringify(temp))
.then(res => {
console.log(res.data.data)
}).catch(err => {
console.log(err)
})
}
</script>
后端:
@PostMapping("/query")
public RestResponse queryTest(User user){
System.out.println(user);
return RestResponse.ok("已收到");
}
data(){
return{
ruleForm: {
dname: '',
index: '',
manger: '',
tel:'',
email:'',
cadate:'',
status:''
}
}
}
add(){
let formdata = new FormData();
for(let key in this.ruleForm){
formdata.append(key,this.ruleForm[key]);
}
}
this.$axios.post('http://localhost:8082/depart/add',formdata)
delete
deleteRow(param){
this.$axios.delete('http://localhost:8082/user/delete',{
params:{id:param}
}).then(successRespose =>{
this.init();
}).catch(err=>{
console.log(param)
})
},
get
init(){
this.$axios.get('http://localhost:8082/user/select')
.then(res=>{
this.userData = res.data;
}).catch(err=>{
console.log(res.data)
})
},
带参数的请求:
findByType () {
this.$axios.get('http://localhost:8888/book/findByType', {
params: {
type: this.bookType // type对应的是后端所需要的参数名称
}
})
.then(res => {
console.log(res)
this.tableData = res.data.data
}).catch(err => {
console.log(err)
})
}
后端:
@GetMapping("/findByType")
public RestResponse findByType(@RequestParam("type") String type){
return RestResponse.ok().put("data",bookService.findBookByType(type));
}
put
editForm:{
id:'',
userName:'',
passWord:'',
realName:''
}
let formData = new FormData();
for(let key in this.editForm){
formData.append(key,this.editForm[key])
}
this.$axios.put('http://localhost:8082/user/update',formData)
.then(res=>{
//this.init();
})