Open external url in new tab after Ajax success using JavaScript in ASP.Net MVC

SajidHussa
 
on Sep 22, 2021 10:41 PM
Sample_387773.zip
892 Views

it work but it open in same tab i want open in new tab

Redirect to external url after Ajax success using JavaScript in ASP.Net MVC

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 22, 2021 11:41 PM

Hi SajidHussa,

Refer below code.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetUrl()
    {
        var url = "https://www.aspsnippets.com";
        return Json(url);
    }
}

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <input id="btnGetResponse" type="button" value="Redirect" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGetResponse").click(function () {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("GetUrl", "Home")',
                    data: '{}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                       if (response) {
                           window.open(response, '_blank');
                        }
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
</body>
</html>

Screenshot