```javascript
```javascript
import axios from 'axios';
Vue.prototype.$http = axios
created(){
this.getJoks();
this.getMoives()
},
get方法
getJoks(){
this.$http.get("http://www.520mg.com/mi/list.php",{params:{page:2}}).then(res=>{
console.log("get到的数据",res)
}).catch(err=>{
console.log("出错原因",err)
})
},
post方法
getMoives(){
this.$http.post("http://www.endata.com.cn/API/GetData.ashx", "areaId=50&typeId=0&year=0&initial=&pageIndex=1&pageSize=10&MethodName=BoxOffice_GetMovieData_List",
{
headers:{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
}).then(res=>{console.log("post数据请求",res)}).catch(err=>{
console.log("出错原因",err)
})
}
post(url,k1=v1&k2=v2,
{headers:"Content-Type": "application/x www-form-
urlencoded; charset=UTF-8"}}
url-encoded
.then()
(序列化数据)
.catch
pdst(url,{k1:v1,k2:v2})
.then()
json日.catch)
post
post(url,formData)
.then)
file文件日.catch0
协议一致端口一致,域名一致,子域名一致
http://www.520mg. com:433/list/page . htmL
http协议www 子域名520ng.com 域名:433端口号
1. jsonp (script 标签没有同源限制)
2.服务器响应头信息允许跨域
Access-Control -ALLow-Origin: *
3.本地服务器代理proxy
```javascript
npm install axios -S
npm install qs -S
import axios from 'axios';
import qs from 'qs';
const BASEURL = process.env.NODE_ENV === 'production'?'https://www.xxxxx.com':'';
let request = axios.create({
baseURL:BASEURL,
timeout:5000
})
request.interceptors.request.use(
config=>{config.headers.token = localStorage.getItem("token");return config},
err=>Promise.reject(err)
)
request.interceptors.response.use(
res=>{
return res;
},
err=>Promise.reject(err)
)
request.postURL = function(url,data,option={}){
return new Promise(function(resolve,reject){
request({
url:url,
method:"POST",
data:qs.stringify(data),
...option,
headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",...option.headers},
})
.then(res=>resolve(res))
.catch(err=>reject(err))
})
}
export default request;
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200629191418825.png)
import request from '@/utils/request.js';
function Login(data){
return request.postURL("/member/index_login2.php",data);
}
function Exit(){
return request.postURL("/member/index_login2.php",{dopost:"exit"});
}
function GetJok(){
return request.postURL("/mi/list2.php");
}
function GetUser(){
return request.get("/member/ajax_login.php")
}
import {Login} from '@/api/user.js'
export default {
data(){
return {
user:{fmdo:"login",dopost:"login",userid:"",pwd:""},
}
},
methods:{
login(){
Login(this.user)
.then(res=>{
if(res.data.status==1){
localStorage.setItem("token",res.data.token);
if(this.$route.query.redirect){
this.$router.replace(this.$route.query.redirect);
}else{
this.$router.replace("/user");
}
}
})
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020062919162935.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RhYWlkdWJhbw==,size_16,color_FFFFFF,t_70)