Formdata 和 XMLhttpRequest 在 IE 11 上不起作用

Formdata and XMLhttpRequest not Working on IE 11

本文关键字:不起作用 IE XMLhttpRequest Formdata      更新时间:2023-09-26

我正在使用formdataXMLHttpRequest来使用ajax提交我的表单。现在一切在其他浏览器上运行良好,除了 IE 11 .注意我正在使用此 ajax 请求上传文件。

我的代码如下

var form = document.getElementById('chatMessageForm');
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
// Add any event handlers here...
xhr.open('POST', form.getAttribute('action'), true);
xhr.responseType = "json";
xhr.send(formData);
xhr.onload = function (e) {
    var new_message_response = xhr.response; // not responseText
    console.log(new_message_response);
    if (new_message_response.conversationStatus) {
        alert('This Conversation is disabled by Other User');
        jQuery('.conversationadd .messagebox #msgbox').attr('disabled',true);
    } else {
        var downloadLink = '';
        if (new_message_response.attachment != '' && new_message_response.attachment != null) {
            downloadLink = '<a href=" ' + new_message_response.attachment_file_path + '" download="' + new_message_response.attachment + '" class="attachment">Download Attachment</a>';
        }
        jQuery('.chatmessageinner').append('<div class="singlemsg right" id=" ' + new_message_response.id + ' ">'
            + '<p> ' + msg + ' </p>'
            + '<div class="messagefooter">'
            + '<span class="time">' + new_message_response.time + '</span>'
            + downloadLink
            + '</div>'
            + '</div>');
        var objDiv = document.getElementsByClassName("chatmessageinner")["0"];
        objDiv.scrollTop = objDiv.scrollHeight;
    }
}

我使用了jQuery.ajax而不是XMLhttpRequest,现在一切都像丝绸一样流畅:)现在我的代码如下所示:

var form = document.getElementById('chatMessageForm');
            var formData = new FormData(form);
jQuery.ajax({
                url: form.getAttribute('action'),
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    var new_message_response = data;
                    if (new_message_response.conversationStatus)
                    {
                        alert('This Conversation is disabled by Other User');
                        jQuery('.conversationadd .messagebox #msgbox').attr('disabled', true);
                    }
                    else
                    {
                        var downloadLink = '';
                        if (new_message_response.attachment != '' && new_message_response.attachment != null)
                        {
                            downloadLink = '<a href=" ' + new_message_response.attachment_file_path + '" download="' + new_message_response.attachment + '" class="attachment">Download Attachment</a>';
                        }
                        jQuery('.chatmessageinner').append('<div class="singlemsg right" id=" ' + new_message_response.id + ' ">'
                                + '<p> ' + msg + ' </p>'
                                + '<div class="messagefooter">'
                                + '<span class="time">' + new_message_response.time + '</span>'
                                + downloadLink
                                + '</div>'
                                + '</div>');
                        var objDiv = document.getElementsByClassName("chatmessageinner")["0"];
                        objDiv.scrollTop = objDiv.scrollHeight;
                    }
                },
                error: function (data) {
                    alert("error");
                }
            });