Open in a new window with Ajax

0

I have a code in ajax that I did not create, and I do not know how to modify it so that the link opens in a new tab.

Note: It is a button, which is activated after 5 seconds of waiting, and this is only part of the code that makes up its programming.

$.ajax({
        dataType: 'json', // The type of data that you're expecting back from the server.
        type: 'POST', // he HTTP method to use for the request
        url: goForm.attr('action'),
        data: goForm.serialize(), // Data to be sent to the server.
        beforeSend: function (xhr) {
            submitButton.attr("disabled", "disabled");
            $('a.get-link').text('<?= __('Getting link...') ?>');
        },
        success: function (result, status, xhr) {
            //console.log( result );
            if (result.url) {
                //console.log( result.message + ' - ' + result.url );
                $('a.get-link').attr('href', result.url).removeClass('disabled').text('<?= __('Get Link') ?>');
                //submitButton.text( 'Redirecting...' );
                //goForm.replaceWith( '<button class="btn btn-default" onclick="javascript: return false;">Redirecting...</button>' );
            } else {
                alert(result.message);
            }
        },
        error: function (xhr, status, error) {
            alert("An error occured: " + xhr.status + " " + xhr.statusText);
        },
        complete: function (xhr, status) {

        }
    });
});
    
asked by ByBrayanYT - Tops y más 01.12.2017 в 22:04
source

2 answers

0

To make the link open in a new window you must use window.open ()

$.ajax({
        dataType: 'json', // The type of data that you're expecting back from the server.
        type: 'POST', // he HTTP method to use for the request
        url: goForm.attr('action'),
        data: goForm.serialize(), // Data to be sent to the server.
        beforeSend: function (xhr) {
            submitButton.attr("disabled", "disabled");
            $('a.get-link').text('<?= __('Getting link...') ?>');
        },
        success: function (result, status, xhr) {
            //console.log( result );
            if (result.url) {
                //console.log( result.message + ' - ' + result.url );
                window.open(result.url, "_blank");
                //submitButton.text( 'Redirecting...' );
                //goForm.replaceWith( '<button class="btn btn-default" onclick="javascript: return false;">Redirecting...</button>' );
            } else {
                alert(result.message);
            }
        },
        error: function (xhr, status, error) {
            alert("An error occured: " + xhr.status + " " + xhr.statusText);
        },
        complete: function (xhr, status) {

        }
    });
});
    
answered by 01.12.2017 в 22:09
0

To open the url in another window use:

window.open(result.url, "_blank");
    
answered by 01.12.2017 в 22:11