一、vue跨域(只能用在开发模式,生产模式不行)
修改vue.config.js
proxyTable: {
'/api': { //使用"/api"来代替"http://v.juhe.cn/toutiao/index"
target: 'http://v.juhe.cn/toutiao/index', //源地址
changeOrigin: true, //改变源
pathRewrite: {
'^/api': '' //路径重写
}
}
},
axios请求数据:
this.axios.post("/api?type=keji&key=yourkey").then(res => {
console.log("api:"+res);
this.kjnews = res.data.result.data;
});
二、vue中引入jquery,用jsonp(只能Get请求方式,Post不行)
需要服务器端写一个callback方法
methods: {
getData () {
var that = this
$.ajax({
url: 'yoururl',
type: 'GET',
dataType: 'JSONP',
success: function (res) {
that.data = res.data;
}
})
}
}
三、后台更改header(全面支持各种情况)
header('Access-Control-Allow-Origin:*');//允许所有来源访问
header('Access-Control-Allow-Method:POST,GET');//允许访问的方式