问题
I'm using xmlHTTPRequest (and also $.ajax) to send a form(that contain some text inputs and multi-file input) without refreshing the page. I'm using FormData to send the form successfully on PC (Chrome). When I run the page in my IPAD (IOS), it didn't work. After trying to know where the error was, it seemed that FormData not supported in it. Is there any alternative for it in my case ?
var formDataH = new FormData($('#FeedbackForm')[0]);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200){
alert(xhttp.responseText);
$('#FeedbackBar').html('<span class="text-success">Sent...</span>');
$('#FeedbackForm')[0].reset();
}else{
$('#FeedbackBar').html('<span class="text-danger">Error Sending, try again...</span>');
}
setTimeout(function() {$('#FeedbackBar').html('');}, 2000);
}
};
xhttp.open("POST", "conf/feedbackpost.php", true);
xhttp.send(formDataH);
回答1:
I was able to use FormData with Safari on iOS 10.3.3 (iPhone 5C) like so:
var formData = new FormData(document.getElementById('form1'));
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(event) {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
// do something
} else {
// do something
}
// clean
xhr = null;
}
}
xhr.open('post', 'http://server/upload.php', true);
xhr.send(formData);
<form id="form1" action="upload.php" method="POST" enctype="multipart/form-data">
<label id="bChoose" for="file">Choose</label>
<input id="file" type="file" name="file" />
</form>
来源:https://stackoverflow.com/questions/51583748/what-is-the-alternative-of-formdata-in-ios