XMLHttpRequest is different in IE8 and FireFox / Chrome

I have a problem similar to jQuery $ .ajax Does not work in IE8, but works on FireFox and Chrome , but with a different use case.

I use the jQuery Form plugin to handle file upload to an ASP.NET MVC controller that sends the file for parsing and processing. If an exception is thrown, it should alert the user about the problem.

//client side code
//make an ajax call, sending the contents of the file
                    $("#ajaxUploadForm").ajaxSubmit({
                        dataType: 'json',
                        url: '/plan/Something/ExcelImport',
                        iframe: true,
                        beforeSend: function () {
                            $(".ui-dialog-buttonpane").find("#my_progress").fadeIn();
                        },
                        success: function (data, textStatus) {
                            output = "<center><span class='flash'>" + data.message + "</span></center>";
                            $("#flash_message").html(output).fadeIn('slow');
                            setTimeout(function () { $("#flash_message").fadeOut() }, 5000);
                            cleanup();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("XMLHttpRequest is " + XMLHttpRequest);
                            var contents = "";
                            for (prop in XMLHttpRequest) {
                                contents += "\na property is " + prop + " it value is " + XMLHttpRequest[prop];
                            }
                            alert("the contents are " + contents);
                            alert("textStatus is " + textStatus);
                            alert("errorThrown is " + errorThrown);
                            //comes back in an HTML envelope. This should be parsed with regex, but I can't get it to work. Dirty hack
                            response = XMLHttpRequest.responseText.substring(XMLHttpRequest.responseText.indexOf("<body>"));
                            response = response.replace("<body>", "");
                            response = response.replace("</body>", "");
                            alert("There was a problem with the upload.\r\n" + response);
                        },
                        complete: function (XMLHttpRequest, textStatus) {
                            $(".ui-dialog-buttonpane").find("#my_progress").remove();
                            something_import.dialog('close');
                            something_import.dialog('destroy');
                        }
                    });

//server side code
public FileUploadJsonResult ExcelImport()
    {
        FileUploadJsonResult result = new FileUploadJsonResult();
        HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
        if (hpf.ContentLength == 0)
            return new FileUploadJsonResult { Data = new { message = "File contained no data" } };
        String fileName = Path.GetFileName(hpf.FileName);
        String timeStampedFile = fileName.Insert(fileName.IndexOf('.'),"_"+DateTime.Now.ToFileTimeUtc());
        string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "tempo", timeStampedFile);
        hpf.SaveAs(savedFileName);
        try
        {
            result = ProcessFile(savedFileName, Request["Id"]) as FileUploadJsonResult;
        }
        catch (ArgumentException e)
        {
            this.Response.StatusCode = 500;
            this.Response.StatusDescription = System.Net.HttpStatusCode.BadRequest.ToString();
            Response.Write(e.Message);
            result = Json(new { message = e.Message, stackTrace = e.StackTrace }) as FileUploadJsonResult;  
        }
        return result;
    }

This works great in Chrome and Firefox. In IE, the returned XMLHttpRequest object is different:

Ff: alt text

IE: alt text

I looked for the difference between the XMLHttpRequest implementations in the browser, but I did not find anything regarding this particular case. Cornered.

+3
2

, , iframe , ajaxSubmit. , , iframe, IE , , , , iframe.

( ), .

json textarea, (IE, FF, Chrome, , Safari), .

.

{Id: 1, Name: 'Me'}

:

<textarea>{Id: 1, Name: 'Me'}</textarea>

, IE html, iframe. ajaxSubmit - json, .:)

ASP.NET MVC, :)

public static class ControllerExtensions
{
    public static ActionResult JsonSafe(this IController controller, object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return new WriteResult(string.Format("<textarea>{0}</textarea>", serializer.Serialize(obj)));
    }
}
+2

wikipedia XMLHttpRequest, , XMLHttpRequest. , Microsoft Mozilla / , , , , .

Microsoft XMLHttpRequest, , , .

Mozilla XMLHttpRequest.

, , W3C XMLHttpRequest, - , ,

Apple Opera XMLHttpRequest.

+1

Source: https://habr.com/ru/post/1767220/


All Articles