Safari 11.1:当input [type = file]为空时,ajax/XHR表单提交失败 [英] Safari 11.1: ajax/XHR form submission fails when input[type=file] is empty

查看:157
本文介绍了Safari 11.1:当input [type = file]为空时,ajax/XHR表单提交失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:从 Webkit版本r230963 开始, Webkit中的问题已解决.

UPDATE: As of Webkit build r230963, this issue has been resolved in Webkit.

===========

===========

由于最近在macOS和iOS以及Safari Technology Preview 11.2上对Safari 11.1进行了更新,因此当input[type=file]字段未选择文件时,我的Web应用程序中的$.ajax调用失败(不需要)以我的形式).当字段确实选择了文件时,不会失败.

Since the recent Safari 11.1 update on macOS and iOS, as well as in Safari Technology Preview 11.2, the $.ajax calls in my web application are failing when a input[type=file] field has no file chosen (it isn't required in my form). No failure when the field does have a file chosen.

运行ajaxerror回调,并且Safari控制台包含以下消息:Failed to load resource: The operation couldn’t be completed. Protocol error.我是HTTPS,并且也通过HTTPS提交到同一域(和服务器)上的某个位置.

The error callback of ajax runs and the Safari console contains the following message: Failed to load resource: The operation couldn’t be completed. Protocol error. I am HTTPS and submitting to a location on the same domain (and server) also over HTTPS.

在11.1更新之前,如果没有选择文件,则提交$.ajax调用就很好.最新版本的Chrome和Firefox没问题.

Before the 11.1 update, the $.ajax call submitted just fine when no file was chosen. The latest versions of Chrome and Firefox have no issues.

我代码的相关部分:

输入:

Browse... <input id="file-upload" type="file" name="image" accept=".jpg,.jpeg">

JS:

var formData = new FormData($(this)[0]);
$.ajax({
    type: 'POST',
    enctype: 'multipart/form-data',
    url: '../process.php',
    data: formData,
    contentType: false,
    processData: false,
    cache: false,
    success: function(response) { ... },
    error: function() { //my code reaches here }
});

作为一个临时的(希望的)解决方案,我正在检测一个空文件字段,并在ajax调用之前将其从formData中删除,并且一切都按预期进行:

As a temporary (hopefully) solution, I'm detecting an empty file field and removing it from formData before the ajax call and everything works as expected/before:

$("input[type=file]").each(function() {
    if($(this).val() === "") {
        formData.delete($(this).attr("name"));
    }
});

我做错什么了吗,Safari是否存在问题,或者Safari是否有变化,现在需要在ajax调用中加以说明?

Am I doing something wrong, is there an issue with Safari, or is there a change in Safari that needs to be accounted for now in ajax calls?

推荐答案

Webkit构建r230963 ,此问题已在Webkit中解决.我下载并运行了该版本,并确认问题已解决.不知道何时可以将包含此修复程序的Safari公开发布.

As of Webkit build r230963, this issue has been resolved in Webkit. I downloaded and ran that build and confirmed the issue is resolved. Not idea when a public release will be available for Safari that contains this fix.

这篇关于Safari 11.1:当input [type = file]为空时,ajax/XHR表单提交失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆