2011-11-30 51 views
1

Asp.Net编程新手,刚刚开始了一个web项目。 是从ASPX页面使用JSON象下面这样调用一个WebMethod:Asp.Net中的Webmethod重定向

<script type="text/javascript"> 

    function getLogin() { 
     var userName = document.getElementById('TextBox1').value; 
     $.ajax({ 
      type: "POST", 
      url: "Services/LogService.asmx/authenticateLogin", 
      data: "{'userName':'" +userName.toString()+ "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
      alert(response.d) 
      }, 
      error: function (xhr, status, error) { 
       // alert(textStatus); 
       DisplayError(xhr); 
      } 

     }); 
    } 


function DisplayError(xhr) { 
var msg = JSON.parse(xhr.responseText); 
alert(msg.Message); } 
</script> 

而且的WebMethod:

[WebMethod] 
    public string authenticateLogin(string userName) 
    { 
     LoginBO loginBO = new LoginBO(); 
     loginBO.userName = userName.ToString().Trim(); 
     string result = ""; 
     try 
     { 
      LoginDao loginDao = DAOFactory.GetDaoFactory().getLoginDao(); 
      result = loginDao.selectUser(loginBO); 
     } 
     catch (DBConnectionException) 
     { 
      //result = "DB Conenction"; 
      throw new Exception("DB Connection is Down"); 
     } 

     catch (InvalidLoginException) 
     { 
      //HttpResponse res = new HttpResponse(); 
      //HttpResponse.ReferenceEquals.Redirect("~/Login.aspx"); 
      throw new InvalidLoginException("Login Is Invalid"); 
     } 
     catch (Exception) 
     { 
      throw new Exception("Uanble to Fetch "); 
     } 
     int ctx = Context.Response.StatusCode; 
     return result; 
    } 

认证成功后,我想用户重定向到另一个aspx页面。

最佳做法是什么?

感谢 塞缪尔

回答

3

添加一个重定向到您的getLogin()功能的成功部分:

success: 
    function (response) { 
    alert(response.d); 
    windows.location.href = "http://url.for.redirect"; 
    } 

(或者使用some other method对于内的jQuery/JavaScript的重定向)。

+0

埃利斯,什么用于重定向window.location.href或window.location.replace? –

+0

请参阅[本文](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery) - 它讨论了不同的选项及其优点/缺点。似乎更喜欢'replace'方法。 –

1

在你的Ajax方法

success: function(msg) { 
     alert(response.d); 
     window.location = "xyz.aspx"; 
    }, 
0
 success: function (response) { 
      alert(response.d) 
     window.location.href = "some.aspx";. 
     }  

我认为这将帮助你。