AJAX request fails when sending FormData() including empty file input in Safari 10.13.4

前端 未结 5 1204
一生所求
一生所求 2020-12-14 10:48

I am running a Symfony 2.8 based web app which sends some form data back to a controller using Ajax.

So far everything worked fine, but since the latest macOS update

5条回答
  •  甜味超标
    2020-12-14 11:29

    I used Andrei's suggestion, which worked for safari, but broke IE.

    The only solution I could find that would work in both browsers was to use eval().

    Since this appears to be a bug only affecting safari 11 I also added a check on browser version.

    if(dataObj instanceof FormData && navigator.userAgent.match(/version\/11((\.[0-9]*)*)? .*safari/i)) {
        try {
            eval('for (var pair of dataObj.entries()) {\
                if (pair[1] instanceof File && pair[1].name === \'\' && pair[1].size === 0) {\
                    dataObj.delete(pair[0]);\
                }\
            }');
        } catch(e) {}
    }
    

提交回复
热议问题