简体   繁体   中英

How call $.post and set content type Multipart/form-data data

I hava a java script function in my JSP page

    function submitProductCategoryForm()
    {
        document.getElementById('isSave').value="1";
        var elements=document.getElementById('addProductCategoryFrom').elements;
        var url = "addProductCategory.jsp?";
       for (var i = 0; i < elements.length; i++)
          url += elements[i].id + "=" + encodeURIComponent($("#" + elements[i].id).val()) + "&";
       $.post(url, function(data)
       {
            alert("Function Complete");
       }); 
   }

It call a jsp page and here I saved the data in DB.

Now on this form there is a file input is also there, so it must be to set the content type to multipart/form-data

but when i call this function i found the error that

java.io.IOException: Posted content type isn't multipart/form-data

my form is like this

<form action="addProductCategory.jsp" method="post" enctype="multipart/form-data" id="addProductCategoryFrom">

So please how i set the content type to multipart.

thanks in advance

I used a FormData object and it seems to do what you want, check my question/solution https://stackoverflow.com/a/21191491/995514

$("#theForm").submit(function(e){
    e.preventDefault();
    var theForm = new FormData($(this)[0]);
    $.ajax({
        url: '.../rest/save',
        type: 'POST',
        data: theForm,
        cache, false,
        contentType: false,
        processData: false
    });
    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM