前后端分离,前端发起请求时,出现了如下错误
XHRPOST
http://localhost:8080/api/account/switchAccount
[HTTP/1.1 403 Forbidden 183ms]
XML 解析错误:语法错误
位置:http://localhost:8080/api/account/switchAccount
疑惑:设置了vue.config.js中的代理,为何端口号是8080,不是8088?另外报出的403该如何解决?
前端请求界面代码
<el-form :model="account" ref="form" label-width="80px">
<el-form-item label="账户文件">
<el-upload action="/api/account/switchAccount" :on-success="handleSuccess">
<el-button slot="trigger" size="small" type="primary">切换账户</el-button>
</el-upload>
</el-form-item>
</el-form>
<script>
export default {
methods: {
handleSuccess(response, file, fileList) {
this.$message.success('上传成功');
}
}
}
</script>
在想后端Java spring boot webapi,时出现了上边提到的错误。
关于vue.config.js代码如下:
const {defineConfig}=require('@vue/cli-service')
module.exports=defineConfig({
devServer:{
proxy:{
'/api':{
target:'http://127.0.0.1:8088',
ws:true,
changeOrigin:true,
pathRewrite:{
'^/api':''
}
}
}
}
})
后端代码如下:
@PostMapping("/account/switchAccount")
@CrossOrigin(origins ="http://127.0.0.1:8080/ChangeAccount")
// @CrossOrigin
public void switchAccount(@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// 使用bytes来保存文件...
} catch (Exception e) {
}
} else {
}
}
后端的端口相关配置:
server.port=8088
恳请大家的指导与帮助