简体   繁体   中英

Continue executing javascript code after window.location is set to call a get method in controller?

I set the window.location to a call a get method in my ASP.NET Controller which returns a FileResult and then starts the download on client side.

How could I continue some code after the file was returned to the page. I can't execute JavaScript code after window.location is set.

The user enters some data in a form and expect the page to return a file as response. The page may not be reloaded. At first I tried calling the controller method (ASP.NET GET) with an Ajax method but I found out that I can't return a file as Ajax result.

So I set the window.location to the action and the download starts just like it should.

The problem that I am not able to solve is that I need to refresh the view by using some of my async Ajax methods after the download.

    function offlineAktivieren(element) {
        // prepare some data
        var id = "xxx";
        var code = "xxx";

        // redirect to post action
        window.location = '/controller/getmethod?id=' + id + '&code=' + code;

        // MY GOAL
        // execute code after window.location
        // refreshContent(element);
    }

I expect code to be executed after the call of window.location. How could i archieve this?

Update: With window.open() I can execute javascript but then i need to wait for eindow.open() to finish somehow.

Instead of using a Windows.location you can call ajax request or just

window.open('/controller/getmethod?id=' + id + '&code=' + code);

So your code will be something like that

function offlineAktivieren(element) {
    // prepare some data
    var id = "xxx";
    var code = "xxx";

    // redirect to post action
    window.open('/controller/getmethod?id=' + id + '&code=' + code);

    // MY GOAL
    // execute code after window.location
    // refreshContent(element);
}

Your windows.location basically change the url but dont executes or loads. try one of the following to make the happen.

window.location.assign(url);
window.location.href = url;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM