当前位置 博文首页 > 后端码匠:Nuxt如何发起跨域资源请求?
npm i @nuxtjs/proxy -D
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
axios: {
proxy: true
},
proxy: {
'/api': {
target: 'http://example.com',
pathRewrite: {
'^/api' : '/'
}
}
}
modules: [
// https://go.nuxtjs.dev/axios
'@nuxtjs/axios',
// https://go.nuxtjs.dev/pwa
'@nuxtjs/pwa',
// https://go.nuxtjs.dev/content
'@nuxt/content',
// 跨域
'@nuxtjs/proxy'
],
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
proxy: true,
common: {
'Accept': 'application/json, text/plain, */*'
},
},
// 跨域
proxy: {
'/api': {
target: 'http://127.0.0.1:3001',
pathRewrite: {
'^/api' : '/'
}
}
},
const Mock = require('mockjs')
let Result = {
code: 200,
msg: "操作成功",
data: null
}
// 获取人物信息
Mock.mock('/api/user', 'get', () => {
Result.data = { "userInfo": { "id": 9979, "userName": "Redmi K30", "productImg": "public/imgs/phone/Redmi-k30.png", "price": 1599, "num": 1, "maxNum": 5, "check": false } }
Result.msg = "购物车错误"
return Result
})
created() {
this.getUserInfo();
},
methods: {
getUserInfo() {
console.log("已经进入了++++++++");
this.$axios
.get("/api/user")
.then((res) => {
this.user = res.data.userInfo;
console.log(JSON.stringify(res.data.data) + "=====res.data.data");
})
.catch((err) => {
return Promise.reject(err);
});
},
},