国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue.js 中 axios 跨域訪問錯誤問題及解決方法

2024-05-06 15:27:52
字體:
供稿:網(wǎng)友

1、假如訪問的接口地址為 http://www.test.com/apis/index.php  (php api 接口)

2、而開發(fā)地址為http://127.0.0.1:8080,當(dāng)axios發(fā)起請求時,出現(xiàn)如下錯誤:

Failed to load http://www.test.com/apis/index.php?&act=login: The value of the 'Access-Control-Allow-Origin' headerin the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://127.0.0.1:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

解決方法:

1、修改config/index.js, 修改 proxytable

proxyTable: { '/apis': {   target: 'http://www.test.com/apis/', // 接口地址   changeOrigin: true,    pathRewrite: {     '^/apis': ''  //需要rewrite重寫的,   }        }},

2、重啟 npm run dev    (很重要,否則不生效)

3、訪問接口(訪問時通過代理轉(zhuǎn)發(fā)的,有點(diǎn)類似APACHE的urlrewrite,不需要完整http://www.test.com網(wǎng)址。)

this.$api.get('/apis/index.php?act=login', {  "act": "login" }, response => {    //success },error => {    //error });

大功告成,成功解決錯誤,另外:PHP端不需要做任何的header設(shè)置,網(wǎng)上很多教程胡說八道,根本不能實(shí)現(xiàn)跨域。

總結(jié)

以上所述是小編給大家介紹的Vue.js 中 axios 跨域訪問錯誤問題及解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對錯新站長站網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 沙坪坝区| 南川市| 株洲市| 固原市| 富锦市| 沙田区| 从化市| 新竹市| 黄浦区| 兴义市| 丹寨县| 青神县| 罗甸县| 始兴县| 方山县| 达孜县| 山东省| 长宁区| 安康市| 莒南县| 安陆市| 九龙县| 远安县| 广平县| 西安市| 那坡县| 弋阳县| 广平县| 宝山区| 上栗县| 吴堡县| 临沧市| 营口市| 诸暨市| 容城县| 中牟县| 城步| 甘肃省| 吉木萨尔县| 象州县| 大名县|