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

本文介绍了一种使用jQuery AJAX实现跨域POST上传文件的方法,并遇到了therequestwasrejectedbecausenomultipartboundarywasfound异常。文章提供了完整的HTML与JavaScript代码示例,并寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有个项目要求跨域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>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值