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

1

7

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

        }
    });
});
Disused answered 24/8, 2015 at 14:8 Comment(4)
It appears the Step 3 request is being blocked by a cross-origin detection; if it never completes, it could be that it is leaving the browser in an uncertain state and thus unable to attempt the subsequent XHR request. If you were able to confirm/resolve the cross-origin issue, I bet the other problem would solve itself.Schnur
Step 3's xmlhttp 2 request is successful though for uploading the file and I get a success response. In step 4 which is after the file upload, I cannot post to a new same domain controller action method "renderfinaltree/errorcode". In Step 4 I can still post to the controller action from step 2 "Step2,errorCode". This is in my development machine..It is indeed a CORS issue as I get this "SEC7127: Redirect was blocked for CORS request"Disused
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, I am just going to make sure that the rest of the ajax methods in this view are working as expected.Disused
Can you please post piece of code from the fileUploadURL.ashx, specially from HttpPostedFile or context.Server.MapPathNightmare
D
3

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.

Disused answered 28/8, 2015 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.