lundi 24 août 2015

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);
                    }
                });
            }

        }
    });
});

Aucun commentaire:

Enregistrer un commentaire