本文介绍: 后端注意跨域问题https://www.cnblogs.com/suflowers1700218/p/14230852.html

前端

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/jquery.min.js"></script>
	</head>
	<body>
		<form id="uploadForm" enctype="multipart/form-data">
		    <input id="file" type="file" name="file"/>
		    <button id="upload" type="button">upload</button>
		</form>
	</body>
	<script>
	$("#upload").click(function () {
	            $.ajax({
	                url: 'http://localhost:8081/upload0',
	                type: 'POST',
	                cache: false,
	                data: new FormData($('#uploadForm')[0]),
	                processData: false,
	                contentType: false,
	                success: function (data) {
						console.log(data)
	                    alert(data.msg)
	                }
	            }).fail(function (res) {
					console.log(res)
	                alert("系统错误")
	            });
	        });
	</script>
</html>

后端

注意跨域问题

 /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @CrossOrigin
    @RequestMapping(path = "/upload0", method = {RequestMethod.POST})
    public Result upload0(MultipartFile file) {
        if (!file.isEmpty()) {
            String upload = FileUtil.fileUpload(file);
            System.out.println(upload);
            return  Result.init(200, "上传成功", null);
        }
        return  Result.init(-200, "上传失败", null);
    }

https://www.cnblogs.com/suflowers1700218/p/14230852.html

原文地址:https://blog.csdn.net/qq_40711092/article/details/126257603

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_35150.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注