2017-10-12 82 views
0

我想从jQuery调用mvc控制器操作方法,并且在调用操作方法时,应将用户转移到另一个站点。我已经尝试使用jquery ajax来做到这一点,但在response.redirect之后,我的页面仍然保持在没有任何更改的同一个URL上。从Jquery调用操作方法并将用户重定向到外部网站

$.ajax({ 
     url: '/Controller/Action', 
     success: function (Data) { 
     //don't want to use this callback as I require only calling the 
     action method 
     } 
    }); 

,并在控制器

public void Action(){ 
    // process the request and redirect 
    Response.Redirect("url", false); 
} 

谁能帮助我理解这里的问题是,在上面的代码。

在此先感谢。

+0

'window.location.href = Data'? – guest271314

+0

你可以在你的成功方法中使用'window.location.href',你可以使用它重定向到任何地方。 –

+0

我认为这将为你返回重定向(“http://www.google.com”); –

回答

0

你可以尝试这样的事情

return Redirect("http://www.google.com"); 

,或者尝试使用JavaScript

public ActionResult Index() 
{ 
return Content("<script>window.location = 'http://www.example.com'; 
</script>"); 
} 

您可以检查此链接了解重定向VS RedirectResult

return new RedirectResult() vs return Redirect()

0

我试着它正在工作。我希望这有助于:-)

HTML

<button class="btn btn-primary" onclick="RedirectTosite()">Redirect To Google</button> 

jQuery的呼叫

function RedirectTosite() { 
    $.ajax({ 
     url: '/Employee/RedirectTosite', 
     success: function (Data) 
     { 
      window.location = Data;    
     } 
    }); 
} 

控制器

public JsonResult RedirectTosite() 
    { 
     return Json("http://www.google.com", JsonRequestBehavior.AllowGet); 
    } 
0

我没有得到一个线索,为什么你叫服务器使用AJAX,如果你不做任何数据操作的数据库,如果你的目标只是重定向到一个页面简单地使用JavaScript代码重定向像

window.location.href = '/Controller/Action/' 

好的我确信你已经在void方法上做了一些工作,如果你在void方法上重定向页面,那么ajax请求失败,并且你没有被重定向到另一个页面。它的工作,你只需从控制器中删除的代码

Response.Redirect("url", false); 

行,写一行代码AJAX的成功函数内如下。

window.location.href = '/Controller/Action/' 
相关问题