简体   繁体   English

如何在ajax中使用formdata追加序列化

[英]how to append serialize with formdata in ajax

I am tring to append serilize with formdata .It is not working.My controller as two viewmodel with httppostfilesbase as a parameter.i want to append both serilize collection with formdata and i went to send all data including file to controller.it is not working for me.can any one help on this please` formdata附加serilize 。它不工作。我的控制器作为两个viewmodel与httppostfilesbase作为参数。我想要附加serilize集合与formdata我去发送包括文件的所有数据到controller.it不工作对我来说,请给我任何一个帮助

var fileData = new FormData();

    if (window.FormData !== undefined) {
                    var fileUpload = $("#myFile").get(0);
                    var files = fileUpload.files;

                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);

                    }
                }

            }

        var other_data = $('form').serializeArray();
        fileData.append('file',other_data );
       debugger
        $.ajax({
            type: "POST",
            url: '@Url.Action("Save", "Settlement")',
            data: fileData[0],
            contentType: false,
            processData: false,
            success: function (result) {
                if (result.redirectTo) {

                } else {

                    $("#childcontent").html(result);
                }
            }
        })
    }
}

There is no need of fileData[0] . 不需要fileData[0] Change url to url: '/Settlement/Save', 更改urlurl: '/Settlement/Save',

Also check the console for any errors and revert if you need more information 还要检查控制台是否有任何错误,如果需要更多信息,请恢复

Can you try with this? 你能尝试一下吗?

$.ajax({
    type: "POST",
    url: '/Settlement/Save',
    contentType: false,
    processData: false,
    data: fileData,
    success: function(result) {
        if (result.redirectTo) {

        } else {

            $("#childcontent").html(result);
        }
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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