2017-10-12 101 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/' 
相關問題