JorgeT
JorgeT

Reputation: 23

How to wait within an ajax call with jquery

What I need to do its first call a URL to generate a token then open another page of the (same) site once the token its generate. The problem its that the windows that generates the token close fast so I can't get to authenticate propertly

this my actual code

function configurarClickLinkPromociones() {
    $("#promocionesLink", contextoServicios).click(function () {
        var winRef;
        var seccionUrl = $(this).attr("data-url-seccion");

        if (seccionUrl != "") {

            showLoading();
            $.ajax({
                url: '/Support/GetToken',
                type: "GET",
                dataFormat: "text/json",
                success: function (data) {
                    HideLoading();
                    winRef = window.open(data.Url + data.Token, '', 'menubar=0,resizable=1,location=1,scrollbars=1,width=976,height=505');
                    winRef.close();
                    winRef.open(seccionUrl, '', 'menubar=0,resizable=1,location=1,scrollbars=1,width=976,height=505');
                },
                error: function (XMLHttpRequest) {
                    HideLoading();
                    eval(XMLHttpRequest.responseText);
                }
            });
        }
    });
}

Upvotes: 0

Views: 87

Answers (2)

m59
m59

Reputation: 43785

Just call window.close() in the new window whenever the operation is complete. See this demo (click).

In this demo, my new window just waits, then closes itself:

setTimeout(function() {
  window.close();
}, 1000);

But, you could use an ajax success callback instead:

$.ajax({
  url: 'some/path',
  success: function() {
    window.close();
  }
});

Upvotes: 1

Jai
Jai

Reputation: 74738

its just for testing purpose, you can use an alert this way:

winRef = window.open(data.Url + data.Token.....);
alert(data.Token);  //<-----here
winRef.close();

or you can use setTimeout this way:

winRef = window.open(data.Url + data.Token.....);
setTimeout(function(){
    winRef.close();
    winRef.open(seccionUrl, '',...);
},2000);

Upvotes: 0

Related Questions