【问题标题】:Formdata and XMLhttpRequest not Working on IE 11Formdata 和 XMLhttpRequest 在 IE 11 上不起作用
【发布时间】:2015-03-13 09:14:15
【问题描述】:

我正在使用 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;
    }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。我已经用正确的缩进编辑了你的代码,并从问题中删除了一些不必要的文本。您可以通过准确指出哪些不工作以及您可能在浏览器中收到的任何错误消息来进一步改进这一点。
  • @StephenMuecke 谢谢,我很感激

标签: javascript internet-explorer xmlhttprequest form-data


【解决方案1】:

我使用了 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");
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2018-05-15
    • 2015-05-06
    • 2016-10-13
    • 2018-03-29
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多