the request was rejected because no multipart boundary was found??求解

最近有个项目要求跨域post来上传文件,但是运行代码以抛出the request was rejected because no multipart boundary was found异常,以下是jsp页面的代码,哪位大神可以告诉我是怎么回事,该如何修改代码?

<%@ page language="java" pageEncoding="UTF-8"%>

<html>
<head> 
<title>Upload</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
}; 


$(function(){ 
$('.sumbit').click(function(){
var formData = new FormData($('#form1'));

var jsonuserinfo = $('#form1').serializeObject();  
        alert(JSON.stringify(jsonuserinfo));
        alert(formData);
        $.ajax({
            type: "post", // 或者 "get"
            url: "http://localhost:8020/SmartHomeCloudService/DeviceDriver/Upload",        
            dataType: "json",                   
            data:formData,
            contentType:"multipart/form-data", 
            crossDomain:true,//跨域
            processData:false,
            success:function(){
            alert("success"); 
              },  
           error:function(){  
               alert("error");  
           }  
           });
      
});
});
</script>
</head>
<body>
 <h3 align="center">jquery Ajax</h3>     
<iframe name="upload" style="display:none"></iframe>
<form id="form1" name="form1" method="post" target="upload" action="" encType="multipart/form-data">
<p></p>
   file:<input type="file" name="upload_file" id="upload_file" />
<p></p>
<input class="sumbit" type="button" value="提交">
</form>


</body>
</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
后端报错 "the request was rejected because no multipart boundary was found" 是由于请求中缺少 multipart boundary 导致的。multipart 是一种在 HTTP 请求中传输二进制数据的方式,boundary 是用来分隔不同部分的标识。在处理上传文件时,后端需要解析请求的 multipart 数据,但没有正确的 boundary 信息,导致解析失败。 解决这个问题的方法是确保请求中包含正确的 multipart boundary。可以按照以下步骤进行处理: 1. 首先,确认请求是以 multipart/form-data 格式发送的。这是一种常见的用于上传文件的格式。如果请求是以其他格式发送的,请确保使用正确的格式。 2. 检查请求头中的 Content-Type 字段,确保其值为 multipart/form-data,并且包含正确的 boundary 信息。boundary 的格式通常为 "boundary=xxx",其中 xxx 是一个随机生成的字符串。 3. 如果使用的是框架或库来处理 multipart 数据,例如 Spring 的 MultipartFile 或 Apache Commons FileUpload,确保配置和使用正确。特别是在配置文件上传相关的参数时,要注意设置正确的 boundary。 4. 如果以上步骤都没有解决问题,可以尝试重新生成请求,确保正确设置 multipart boundary。可以使用专门的工具或库来生成包含正确 boundary 的请求。 总结起来,解决后端报错 "the request was rejected because no multipart boundary was found" 的关键是确保请求中包含正确的 multipart boundary,并正确配置和使用处理 multipart 数据的框架或库。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值