javascript 请求被拒绝,因为没有找到多部分边界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11811232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:25:46  来源:igfitidea点击:

The request was rejected because no multipart boundary was found

javajavascriptjakarta-eejquery

提问by Ashish Panery

I am using a Form in a LightBox which contains some input element.

我在包含一些输入元素的 LightBox 中使用表单。

<form name="imageUploadForm" action="uploadImage.do" method="post" enctype="multipart/form-data">
<input type="text"  id="id" name="id" style="display: none;" value="">
    <div id="fileUploaderDiv">
         <input type='file' name="file0" id ="file0"  />
    </div>
<input type="submit" value="Submit">
</form>

when i am submitting the form than the form redirect to it's action location. I just want to submit form without redirecting user, so user stay on lightbox without loosing his data.

当我提交表单时,表单会重定向到它的操作位置。我只想提交表单而不重定向用户,这样用户就可以留在灯箱上而不会丢失他的数据。

I have tried jquery ajax call for this

我为此尝试了 jquery ajax 调用

var data = new FormData();
var $inputs = $('#imageUploadForm :input');
var values = {};
    $inputs.each(function() {
                values[this.name] = $(this).val();
                data.append(this.name, $(this).val());
            });
$.ajax({
                url: 'uploadImage.do',
                data: data,
                cache: false,
                contentType: 'multipart/form-data',
                processData: false,
                type: 'POST',
                success: function(data){
                   alert(data);
                }
            });

But getting error at server side in my FileUploader servlet.

但是在我的 FileUploader servlet 中的服务器端出错。

The request was rejected because no multipart boundary was found 

can anybody tell me what am i missing in this ?

谁能告诉我我错过了什么?

采纳答案by Gogol

Here is the simplest form of a form submit using jquery Ajax. I have not tested this but it should work :-)

这是使用 jquery Ajax 提交表单的最简单形式。我还没有测试过这个,但它应该可以工作:-)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test form</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#sbmt').click(function(){
            var text1 = $('#text1').val();
            var text2 = $('#text2').val();
            /// validation of inputs
            ///then
            $.ajax(
            {
                url :'submit.php',
                type : 'post',
                        data :  {'text1' : text1, 'text2' : text2 },
                success: function(data){
                        alert("form submitted . response is :"+ data);
                    }
            }
            ).fail(function(){alert("Failed!!");});
            });
        });
    </script>
</head>
<body>
    <form id="myform">
        <input type="text" id="text1" />
        <input type="text" id="text2" />
        <input type="button" id="sbmt"  value="submit"/>
    </form>
</body>
</html>

回答by adeneo

You need to prevent the default action of submitting the form:

您需要阻止提交表单的默认操作:

$('form[name="imageUploadForm"]').on('submit', function(e) {
     e.preventDefault();
     $.ajax({
            type: 'POST',
            url: 'uploadImage.do',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data){
               alert(data);
            }
     });
});

I believe you should set the contentType option to false when using the FormData class, forcing jQuery not to add a Content-Type header, otherwise the boundary string will be missing, and that's probably the reason for your server error.

我相信你应该在使用 FormData 类时将 contentType 选项设置为 false,强制 jQuery 不要添加 Content-Type 标头,否则边界字符串将丢失,这可能是你的服务器错误的原因。

You must also leave the processData flag set to false, otherwise, jQuery will try to convert your FormData into a string, which will fail.

您还必须将 processData 标志设置为 false,否则,jQuery 会尝试将您的 FormData 转换为字符串,这将失败。