Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to post to a new action method via .Ajax right after a successful XMLHttpRequest in IE 10

I am adding a new feature in my app which lets users to complete a process in steps.

Step1 select dropdown value

hit next button and step 2 (ajax request loads and renders a partial view, rendering a tree view)

hit next button

step 3 (upload a file via XMLHttpRequest)

hit next button

Step 4(another ajax request is made to render a partial view) For some reason this request will never hit the controller action method. Whats weird is if I post to the action method in step 2, it will post successfully but I have different action for this step.

I am getting the following warnings in IE 10 Developer tools

SEC7127: Redirect was blocked for CORS request.

XMLHttpRequest: Network Error 0x800c0014, A redirection problem occurred. SCRIPT7002: XMLHttpRequest: Network Error 0x800c0014, A redirection problem occurred.

The above errors seem to be related to the XMLhhtprequest before this step. I have tried to set the XMLhttprequest to NULL or try to dispose it upon its success event. I dont understand in Step 4 I can still post to step 2's action but not a different one?

step 3

  function handleFiles() {

        var formdata = new FormData(); //FormData object
        var xhr = new XMLHttpRequest();
        xhr.open('POST', fileUploadURL);
        xhr.setRequestHeader('Content-type', 'multipart/form-data');

        //Appending file information in Http headers
        xhr.setRequestHeader('X-File-Name', document.getElementById('myFile').files[0].name);
        xhr.setRequestHeader('X-File-Type', document.getElementById('myFile').files[0].type);
        xhr.setRequestHeader('X-File-Size', document.getElementById('myFile').files[0].size);

        //Sending file in XMLHttpRequest
        xhr.send(document.getElementById('myFile').files[0]);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                $("input[id=filestoredname]").val(xhr.responseText);
                alert("file saved..");

            }
        }
                return false;
    }

var instrumentSelectionUrl = '@Url.Action("Step2", "ErrorCode")';//step2
var finalTreeViewUrl = '@Url.Action("renderFinalTree", "ErrorCode")';//step3
var fileUploadURL = '@Url.Action("UploadFiles", "ErrorCode")';//step3


$(function () {
    $('form').stepy({
        backLabel: '<<',
        nextLabel: '>>',
        finishButton: false,
        next: function (index) {
            alert(index);
            var v = $("#InsIdLst").chosen().val();
            if (v == null && index == 2) {
                alert("Please select an Instrument");
                return false;
            }
            if (v != null && index == 2) {
                var overlay = $('<div></div>').prependTo('body').attr('id', 'overlay');
                $.ajax({
                    type: 'POST',
                    url: instrumentSelectionUrl,
                    cache: false,
                    datatype: "html",
                    data: $("form").serialize(),
                    success: function (result) {
                        $("#errorCodes").html(result);
                        overlay.remove();
                    }
                });
            }
            if (index == 4) {
                alert("try..");
                $.ajax({
                    type: 'POST',
                    url: finalTreeViewUrl,
                    cache: false,
                    datatype: "html",
                    data: $("form").serialize(),
                    success: function (result) {
                        $("#errorCodesSave").html(result);
                    }
                });
            }

        }
    });
});
like image 270
tam tam Avatar asked Aug 24 '15 14:08

tam tam


1 Answers

So in Step 4 if I use

$("#errorCodesSave").load(finalTreeViewUrl, { fileName: $("#filestoredname").val(), $("#InsIdLst").chosen().val();: 1 }); 

instead of $Ajax, it does post to the intended controller action. This works good enough for me.

like image 73
tam tam Avatar answered Nov 11 '22 01:11

tam tam