axios
{data: 3, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data: 3
headers: {content-type: "text/html; charset=UTF-8"}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"
__proto__: Object
注意:anxios返回的是promise对象
案例如下:
//这里引用了bootstrap
//html文件如下:
<div id="app">
<div class="container">
<div class="row">
格式如下:axios.get(url).then(res=>console.log(res).catch(error=>conosle.log(error))
new Vue({
el:'#app',
methods:{
------------方法1-----------------
get(){
var p = axios.get('localhost/php/get.php?a=1&&b=1');//参数直接放在url后面
p
.then( res => console.log(res))
.catch( e => {
if(e) throw e
})
}
----------方法2--------------
get(){
anxios({
url:'localhost/php/get.php',
method:'get',//默认是get请求,可以省略
params:{ //params中跟着的是get请求的参数
a:1,
b:1
}
})
.then( res => console.log(res))
.catch( e => {
if(e) throw e
})
}
}
})
注意:anxios返回的是promise对象
//这里引用了bootstrap
//html文件如下:
<div id="app">
<div class="container">
<div class="row">
new Vue({
el:'#app',
methods:{
post(){
let params = new URLSearchParams();
params.append('a',5);//传参
params.append('b',5);
anxios({
url:'localhost/php/post.php',
method:'post',
data:params,
headers:{
'Content-Type': "application/x-www-form-urlencoded"
}
})
.then( res => {console.log(res)})
.catch( e => {
if(e) throw e
})
}
}
})
注意fetch返回的是一个promise对象
fetch('./data.json')
.then(res=>{
res.json() //res.text() res.blob()//数据格式化
})
.then( data => console.log(data))
.catch( error => console.log( error ))
//html文件如下:
<div id="app">
<div class="container">
<div class="row">
new Vue({
el:'#app',
methods:{
get(){
fetch('http://localhost/get.php?a=1&b=2')
.then( res => res.text())//对数据进行格式化 res.json() res.blob()
.then( data => console.log(data)
.catch( e => {
if(e) throw e;
})
}
}
})
new Vue({
el:'#app',
methods:{
post(){
var fetch_post = fetch('http://localhost/post.php',{
method:'post',
headers: new Headers({//解决跨域
'Content-Type': "application/x-www-form-urlencoded"
}),
body: new URLSearchParams([ // 进行参数的修改
['a',1],
['b',1]
]).toString()
})
.then( res => res.text())
.then( data => console.log(data))
.catch(e =>{
if(e) throw e
})
}
}
})
new Vue({
el:'#app',
methods:{
json(){
fetch('./data/data.json')
.then( res => res.json())
.then( data => console.log(data))
.catch(errpr => {
if(error) throw error
})
}
}
})