跨域问题
方式一:添加vue.config.js
(最常用)
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8877', // 后端服务器的地址
changeOrigin: true, // 是否改变原始主机头为目标URL
ws: true, // 是否代理websockets
pathRewrite: {'^/api': ''} // 重写路径,去掉'/api'前缀
}
}
}
}
方式二:在manifest.json
中添加
"h5": {
"devServer": {
"proxy": {
"/api": {
"target": "http://localhost:8877", // 后端服务地址
"changeOrigin": true,
"pathRewrite": {
"^/api": ""
}
}
}
}
}
方式三:
服务器添加
router.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*"); // 允许所有域名访问
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});