Suave Nti
Suave Nti

Reputation: 3739

Redirect from Webmethod in Asp.Net

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

Upvotes: 1

Views: 14024

Answers (3)

tareq_moinul
tareq_moinul

Reputation: 109

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

I think it will help you.

Upvotes: 0

FosterZ
FosterZ

Reputation: 3911

In your Ajax method

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

Upvotes: 2

Yaakov Ellis
Yaakov Ellis

Reputation: 41490

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

Upvotes: 4

Related Questions