【问题标题】:Jquery ajax call on IOS return status 400IOS上的jQuery ajax调用返回状态400
【发布时间】:2018-11-12 20:00:43
【问题描述】:

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

 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 在内的网络浏览器中运行良好 它在安卓操作系统中运行良好

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

【问题讨论】:

  • 您是否在 info.plist 中将 AllowsArbitraryLoads 设置为 YES

标签: android jquery ios ajax file-upload


【解决方案1】:
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]);
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多