1

Am new to Asp.Net Programming, Have just started a web project. Am calling a WebMethod from Aspx page using JSON like below:

 <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>

And 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;
    }

After Successful Authentication, I want to redirect user to another aspx page.

What is the best practice to do ?

Thanks Samuel

Suave Nti
  • 3,721
  • 11
  • 54
  • 78

3 Answers3

4

Add a redirect to the success section of your getLogin() function:

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

(Or use some other method for redirecting within jQuery/Javascript).

Community
  • 1
  • 1
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
  • Ellis,what to use for redirect window.location.href or window.location.replace? – Suave Nti Nov 30 '11 at 08:12
  • See [this post](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery) - it discusses the different options and their pros/cons. Seems to prefer the `replace` method. – Yaakov Ellis Nov 30 '11 at 08:26
2

In your Ajax method

success: function(msg) {
      alert(response.d);
        window.location = "xyz.aspx";
  },
FosterZ
  • 3,863
  • 6
  • 38
  • 62
0
       success: function (response) {
          alert(response.d)
         window.location.href = "some.aspx";.
        }    

I think it will help you.

tareq_moinul
  • 109
  • 3