webpack開發(fā)跨域問題解決辦法
本文介紹了webpack開發(fā)跨域問題解決辦法,分享給大家,具體如下:
1. 說明
webpack 內(nèi)置了 http-proxy-middleware 可以解決 請求的 URL 代理的問題
2. API
需要代理的 pathname 要加 /
module.exports = {
devtool: 'cheap-module-source-map',
entry: './app/js/index.js'
output: {
path: path.resolve(__dirname, 'dev'),
// 所有輸出文件的目標(biāo)路徑
filename: 'js/bundle.js',
publicPath: '/',
chunkFilename: '[name].chunk.js'
},
devServer: {
contentBase: path.resolve(__dirname, 'dev'),
publicPath: '/',
historyApiFallback: true,
proxy: {
// 請求到 '/device' 下 的請求都會被代理到 target: http://debug.xxx.com 中
'/device/*': {
target: 'http://debug.xxx.com',
secure: false, // 接受 運(yùn)行在 https 上的服務(wù)
changeOrigin: true
}
}
}
}
3. 使用
注:使用的url 必須以/開始 否則不會代理到指定地址
fetch('/device/space').then(res => {
// 被代理到 http://debug.xxx.com/device/space
return res.json();
}).then(res => {
console.log(res);
})
fetch('device/space').then(res => {
// http://localhost:8080/device/space 訪問本地服務(wù)
return res.json();
}).then(res => {
console.log(res);
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript+CSS實(shí)現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果
這篇文章主要介紹了JavaScript+CSS實(shí)現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果,涉及javascript鼠標(biāo)事件及頁面元素動態(tài)操作的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JavaScript學(xué)習(xí)筆記之?dāng)?shù)組去重
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之?dāng)?shù)組去重的相關(guān)資料,需要的朋友可以參考下2016-03-03
微信小程序scroll-view實(shí)現(xiàn)滾動到錨點(diǎn)左側(cè)導(dǎo)航欄點(diǎn)餐功能(點(diǎn)擊種類,滾動到錨點(diǎn))
這篇文章主要介紹了微信小程序scroll-view左側(cè)導(dǎo)航欄點(diǎn)餐功能實(shí)現(xiàn),點(diǎn)擊種類,滾動到錨點(diǎn);滾動到錨點(diǎn),種類選中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Three.js中的紋理圖像應(yīng)用和屬性調(diào)整方法
在three.js中紋理貼圖是用來給物體表面添加圖案、顏色或者其他視覺效果的一種技術(shù),這篇文章主要給大家介紹了關(guān)于Three.js中紋理圖像應(yīng)用和屬性調(diào)整的相關(guān)資料,需要的朋友可以參考下2024-01-01

