谈谈 HTTP 的请求和响应
https://juejin.cn/post/6844903902773542926
前端向后端获取数据的方法(待)
方法一:jQuery 中的 ajax
GET方法
$ajax.({
url:"请求路径(数据放在url)",
type:"GET",
success:(result)=>{
return result
}
})
POST方法
$.ajax({
url:"请求路径"
type:"POST",
contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
data:{ //根据contentType把data序列化成合适的格式
name:""
password:""
},
success:(result)=>{
return result
}
})
方法二:axios 的方式
设置全局的 axios 默认值
axios.defaults.baseURL = "路径"
axios.defaults.header.common['Authorization'] = AUTH_TOKEN
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
GET方法
import axios from 'axios'
axios({
url:"请求路径"
method:"get",
params:{
name:"",
password:""
}
})
.then(result => {
return result.data
})
.catch(error => {
return error
})
POST方法
import axios from 'axios'
axios({
url:"请求路径"
method:"get",
data:{
name:"",
password:""
}
})
.then(result => {
return result.data
})
.catch(error => {
return error
})
方法三:fetch 方法(兼容性差)
参考文章:https://zhuanlan.zhihu.com/p/89089088 > https://forum.vuejs.org/t/axios-token/33528 > https://www.kancloud.cn/yunye/axios/234845 > https://www.cnblogs.com/ranyonsue/p/12029277.html > https://zhuanlan.zhihu.com/p/351458729 > https://blog.csdn.net/weixin_36617251/article/details/99691144