日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nuxt如何发起跨域资源请求?

發布時間:2024/9/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nuxt如何发起跨域资源请求? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用于 Nuxt.js 的 http-proxy 中間件解決方案

npm i @nuxtjs/proxy -D

在 nuxt.config.js 配置文件中添加對應的模塊,并設置代理

modules: ['@nuxtjs/axios','@nuxtjs/proxy'],axios: {proxy: true},proxy: {'/api': {target: 'http://example.com',pathRewrite: {'^/api' : '/'}}}

實戰

nuxt.config.js

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-axiosaxios: {proxy: true,common: {'Accept': 'application/json, text/plain, */*'},},// 跨域proxy: {'/api': {target: 'http://127.0.0.1:3001',pathRewrite: {'^/api' : '/'}}},

mock.js

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);});},},

截圖

總結

以上是生活随笔為你收集整理的Nuxt如何发起跨域资源请求?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。