報錯信息:Access to XMLHttpRequest at 'http://127.0.0.1:3652/' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
原因:百度上面一大堆,我就不說了;
解決辦法:
一、從后端解決(我用的flask,所以就用flask說吧):安裝flask_cors庫,如下即可
from flask_cors import CORS
import logging
app = Flask(__name__)
# CORS(app)
二、從前端解決這個辦法:
在config文件里面的index.js里面添加一段代碼:在module.exports里面添加一段代碼,標紅的
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: "http://0.0.0.0:3652", #你后端的接口ip加端口,這里相當於baseURL
changeOrigin: true, #允許跨域
pathRewrite: {
'^/api': '/' #以api開頭的請求
}
}
},
然后在請求中也需要改,寫的時候不用加baseURL了(我就是坑到這里了),例如
created() {
// axios.get('http://127.0.0.1:3652').then((res)=>{ #這里是原來的,就是坑到這里了
axios.get('/api').then((res)=>{ #現在用這個,完美運行
console.log(res),
alert(res.data.return_info)
})
}