fetch-jsonp 数据请求跨域
一、安装、配置
1. 安装 npm install fetch-jsonp
2. main.js文件导入 import fetch from 'fetch-jsonp'
3. 当前组件使用 import FetchJsonp from "fetch-jsonp";
二、使用
FetchJsonp(src, {
jsonpCallback: "cb", //指定回调函数的参数
jsonpCallbackFunction: "getData", //指定回调函数的函数名称
timeout: 2000 //请求超时
})
.then(res => {
console.log(res);
// {ok: true, json: ƒ}
// json: ƒ json()
// ok: true
// __proto__: Object
return res.json();
})
.then(res => {
//success
console.log(res.s);
})
.catch(error => {
console.log(error);
});
三、全局
全局引入:
Vue.prototype.$fetch=Fetch;
this.$fetch("")