我使用create-react-app来做我的前端工作,并在antd的上传中使用Dragger组件。我的后端是springboot。我的前端使用fetch向后端发送请求数据,这会导致跨域问题。所以我在create-react-app的package.json文件中添加了" proxy“:"http://localhost:8080”来代理我的后端,解决了跨域的麻烦,但是当我使用antd官网时,上传的拖拽组件总是在上传的时候报告错误。
这是我使用Antd的Dragger的代码。
import React, { Component } from 'react';
import { Upload, Icon, message } from 'antd';
import './UpVideo.css';const Dragger = Upload.Dragger;
导出默认类UpVideo扩展了组件{
render(){
const props = {
name: 'file',
multiple: false,
headers:{
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods':'POST',
},
action: 'http://localhost:8080/vidoe/up',
onChange(info) {
// const status = info.file.status;
console.log(info);
// if (status !== 'uploading') {
// console.log(info.file, info.fileList);
// }
// if (status === 'done') {
// message.success(`${info.file.name} file uploaded successfully.`);
// } else if (status === 'error') {
// message.error(`${info.file.name} file upload failed.`);
// }
},
};
return(
<div>
<Dragger {...props}>
<p className="ant-upload-drag-icon">
<Icon type="inbox" />
</p>
<p className="ant-upload-text">点击或者拖动视频文件到这里</p>
</Dragger>,
</div>
)
}}
这是我给出的错误图片。

对不起,我刚学会使用StackOverflow。如果我的描述不清楚,请让我知道,这个问题已经困扰我很长时间了,谢谢。
发布于 2019-05-07 06:53:51
这个错误是google chrome的一个安全措施。当您使用两个不同的服务器作为前端和后端时,它会出现。安装此库以修复此问题:
npm安装cors https://www.npmjs.com/package/cors
https://stackoverflow.com/questions/53376630
复制相似问题