错误说 POST 请求不包含 multipart/form-data 或 multipart/mixed 流,即使我明确定义了它

Error says that POST request doesn't contain a multipart/form-data or multipart/mixed stream even though I explicitly define it as such

我正在尝试在 Groovy 服务器页面中实现 link,该页面发送 POST 请求,其中包含他们点击的 link 的文本GSP,然后加载该页面。我已经从 Whosebug 上的其他问题中尝试了几种不同的解决方案来解决这个问题,但每一个最终都会引发 org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null 错误。这里发生了什么?我也对不同的方法感兴趣。

要求:

<script type="text/javascript" src="resources/post.js"></script>
<a href="actions/manage.gsp" onclick="post('/actions/manage', {name: '${file}'});">${file}</a>

post.js:

function post(path, params, method) {
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute('enctype','multipart/form-data').
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

manage.gsp:

<%! static output = "text/html" %>
Testing!
<% log.info("Found ${request.parts.size()} parts") %>

我想你错过了 "input type=file" 错过了。

只是在 form.setAttribute('enctype','multipart/form-data');

之后删除了点
function post(path, params, method) {
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute("enctype","application/x-www-form-urlencoded");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

关于这个概念,我想为您提供:

function post(path, params, method) {
 method = method || "post";
 var formData = new FormData();
 for(var key in params) {
        if(params.hasOwnProperty(key)) {
           formData.append(key,params[key] );
        }
    }

        $.ajax({
            type: method,
            url: path,
            data: formData,
            cache: false,
            contentType: false,
            processData: false
            }).done(function( result ) { 
               //alert(result)  
            });
}