JQUery ajax调用IOS返回状态400

时间:2018-06-03 08:00:39

标签: android jquery ios ajax file-upload

我正在尝试使用以下调用提交带有文件的表单:

 var formData = new FormData(this);
 $.ajax({
    cache: false,
    processData: false,
    contentType: false,
    data: formData,
    type: "POST",
    url: "api/action",
    success: function (msg) {
       console.log(msg);

    },
    error: function (msg) {
        console.log(msg);
    }
});

它在包含safari的网络浏览器中工作正常 它在Android OS中运行良好

但是在IOS中,调用失败并返回错误400

enter image description here

1 个答案:

答案 0 :(得分:0)

var $form = $(this); // <- change this depending on your scope/usecase

        $form.find('input[name][type!="file"], select[name], textarea[name]').each(function(i, e) {
            if ($(e).attr('type') == 'checkbox' || $(e).attr('type') == 'radio') {
                if ($(e).is(':checked')) {
                    formData.append($(e).attr('name'), $(e).val());
                }
            } else {
                formData.append($(e).attr('name'), $(e).val());
            }
        });
        // [type="file"] will be handled separately
        $form.find('input[name][type="file"]').each(function(i, e) {
            if ($(e)[0].files.length > 0) {
                formData.append($(e).attr('name'), $(e)[0].files[0]);
            }
        });