Bootstrap

ajax请求过多内存溢出,javascript

我对ajax没有太多经验,但是在将表单数据发送到ASP控制器时遇到问题。

场景1

将ajax请求中的data设置为

$(this).serialize()

所有表单数据都将发布到控制台而不进行任何页面重定向,但不包括与表单一起发送的任何文件。

方案2

将ajax请求中的data设置为

formdata

所有表单数据都已发布,包括上载的文件,但是页面刷新了,我不想发生这种情况。

这是怎么回事,如何在不刷新页面的情况下发送文件?

// Clear any previous 'success' messages (in case of error)

$('form .btn').on('click', function () {

$('#result').html('');

});

// Submit form

$('form').submit(function () {

// Build form data

var formdata = new FormData();

var fileInput = document.getElementById('fileInput');

for (i = 0; i < fileInput.files.length; i++) {

formdata.append(fileInput.files[i].name, fileInput.files[i]);

}

// Send ajax request

$.ajax({

url: this.action,

type: this.method,

data: formdata, // fails to send files when set to $(this).serialize()

beforeSend: function () {

$('form .btn').prop('disabled', true);

},

success: function (result) {

$('form .btn').prop('disabled', false)

$('form .form-control').val('');

$('#result').html('Thank you. We will contact you shortly.');

$('.validation-summary-errors ul li').css('display', 'none');

},

error: function (result) {

$('form .btn').prop('disabled', false)

alert('An error occurred on the server. Please try again. \n\n If this keep happening, please email us at [email protected]');

}

});

return false;

});

;