Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

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

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 to version 10.13.4 users start to report, that submitting the form does not work anymore in Safari. Other macOS Versions and other browsers on 10.13.4 still work fine, so it seems to be a problem in Safari. Of course I filed a bug report to Apple, but I do not think, that I will ever get feedback from there...

I was able to isolate the source of the problem: Submitting data which includes an empty file input fails:

// safri_bug.html
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
<body>
    <form name="app_booking" method="post" action="/test/submit.php">
        <div><input type="text" id="someValue" name="value"></div>
        <div><input id="thefile" type="file" name="file"></div>
    </form>

    <button id="bSubmit" type="button">Submit</button>

    <script>    
        $(document).ready(function() {              
            $('#bSubmit').click(function() {
                var form = $('form');
                var data = new FormData(form[0]);

                $.ajax({
                    url : '/submit.php',
                    type : 'POST',
                    data : data,
                    contentType: false,
                    processData: false,
                    context : this,
                    success : function(response) {
                            alert('success: ' + response);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                            alert('error: ' + xhr.responseText + ' - ' + thrownError);
                    }
                });
            });
        });
    </script>
</body>
</html>


// submit.php
<?php 
    echo "OK";

Result

  • Submitting the form works fine on all tested browsers and platforms but in Safari in macOS 10.13.4
  • In Safari on macOS 10.13.4:
    • If not file is selected: The Ajax request runs for about 20 seconds (build in timeout?) and than returns with an empty sucess response. The submit.php does NOT get called.
    • If a file was selected: Everything works fine...

So, this seems to be a bug in the latest Safari update? Or is there anything wrong with my code?

Any idea how to prevent this bug?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Andrei Herford's solution will crash other browsers that do not support the entries() method of FormData - using try/catch will only find execution errors, not syntax errors.

Our solution was to use plain JavaScript to remove the empty file input element before creating the FormData object, thus:

for (i = 0; i < form.elements.length; i++) {
  if (form.elements[i].type == 'file') {
    if (form.elements[i].value == '') {
      form.elements[i].parentNode.removeChild(form.elements[i]);
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...