前端開(kāi)發(fā)時(shí),請(qǐng)求后臺(tái)接口經(jīng)常需要跨域,vue-cli實(shí)現(xiàn)跨域請(qǐng)求只需要打開(kāi)config/index.js,修改如下內(nèi)容即可。
//例如要請(qǐng)求的接口url為http://172.3.2.1:8000/look/1module.exports = { dev:{ proxyTable:{ '/api':{ target: 'http://172.3.2.1:8000', changeOrigin: true, pathRewrite: { '^/api': '' } } } }}這時(shí)在你想請(qǐng)求接口的url處,輸入/api/look/1 即可實(shí)現(xiàn)跨域請(qǐng)求。
這時(shí)如果打開(kāi)F12會(huì)發(fā)現(xiàn)請(qǐng)求的url是localhost:8080/api/look/1,這其實(shí)是虛擬從本地請(qǐng)求數(shù)據(jù),這樣就不會(huì)有跨域的問(wèn)題產(chǎn)生了。
一般情況下上面的方法是沒(méi)有問(wèn)題的,要是上面的方法行不通,可以試試這樣寫(xiě):
//例如要請(qǐng)求的接口url為http://172.3.2.1:8000/look/1module.exports = { dev:{ proxyTable:{ '/look':{ target: 'http://172.3.2.1:8000', changeOrigin: true, pathRewrite: { '^/look': '/look' } } } }}這時(shí)在你想請(qǐng)求接口的url處,輸入/look/1 即可實(shí)現(xiàn)跨域請(qǐng)求。
詳情:https://vuejs-templates.github.io/webpack/proxy.html
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選