最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue代理模式解決跨域詳解

 更新時(shí)間:2022年09月14日 08:24:32   作者:曦12  
這篇文章主要介紹了vue代理模式解決跨域詳解的相關(guān)資料,需要的朋友可以參考下

跨域是什么

簡(jiǎn)單的講就是你在一個(gè)地方使用另一個(gè)地方的資源,被瀏覽器給擋下來(lái)了,不讓不用!當(dāng)然,它擋下來(lái)是有自己理由的:為了安全(╬▔皿▔)╯。

解決跨域

我是用vue開發(fā)的,就vue代理模式解決跨域說(shuō)明一下。

1、在vue.config.js中這樣寫:

let devProxy = {
? //獲取ip信息
? '/getIpMsg': {
? ? target: "https://whois.pconline.com.cn/ipJson.jsp",//真實(shí)地址
? ? ws: true,
? ? changeOrigin: true,
? ? pathRewrite: {
? ? ? '/getIpMsg': ''
? ? },
? },
};
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
? transpileDependencies: true,
? publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
? devServer: {
? ? port: 8080,//端口
? ? open: false,//項(xiàng)目啟動(dòng)后是否在瀏覽器自動(dòng)打開
? ? proxy: devProxy//代理服務(wù)器
? },
})
?

target就是自己需要代理的真實(shí)地址getIpMsg你是可以自定義的。

2、創(chuàng)建一個(gè)http.ts(我是ts的,你也可以js):

import axios from "axios";
//創(chuàng)建請(qǐng)求
function createServe(config: any) {
? ? let serve = axios.create({
? ? ? ? timeout: 5000 //超時(shí)
? ? });
? ? //請(qǐng)求攔截器
? ? serve.interceptors.request.use(
? ? ? ? config => {
? ? ? ? ? ? return config;
? ? ? ? },
? ? ? ? error => {
? ? ? ? ? ? return Promise.reject(error)
? ? ? ? }
? ? )
? ? //響應(yīng)攔截器
? ? serve.interceptors.response.use(
? ? ? ? response => {
? ? ? ? ? ? return response;
? ? ? ? },
? ? ? ? error => {
? ? ? ? ? ? return Promise.reject(error)
? ? ? ? }
? ? )
? ? return serve(config);
}
?
export default createServe;

3、創(chuàng)建一個(gè)request.ts:

import createServe from "./http"
?
//獲取ip信息
export function getIpMsg(params = {}) {
? ? return createServe({
? ? ? ? method: "GET",
? ? ? ? url: '/getIpMsg',
? ? ? ? params
? ? })
}

4、這樣使用:

import { getIpMsg } from "@/api/request";
?
function test(){
?? ?getIpMsg()
?? ?.then(res => {
?? ??? ?console.log(res);
?? ?})
}

到此這篇關(guān)于vue代理模式解決跨域詳解的文章就介紹到這了,更多相關(guān)vue代理模式解決跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

德兴市| 彭州市| 海盐县| 米脂县| 南江县| 浪卡子县| 揭阳市| 平乡县| 万全县| 阳谷县| 招远市| 太保市| 朔州市| 天镇县| 公主岭市| 余江县| 岳阳县| 台前县| 彝良县| 翼城县| 镇赉县| 金坛市| 房产| 哈尔滨市| 文昌市| 安丘市| 漠河县| 宿州市| 靖西县| 湘阴县| 杭锦旗| 云浮市| 北宁市| 渝北区| 建湖县| 勐海县| 颍上县| 潞西市| 龙游县| 巴青县| 西乌珠穆沁旗|