nazar tvm
nazar tvm

Reputation: 99

Response.Redirect is not redirecting the page

  $('#logout').on('click', function () {
    console.log("clicked");
    $.ajax({
        type:"GET",
        url: 'Home.aspx?method=simple'
        }).done(function (data) {
        console.log(data);
    });
    //PageMethods.simple();
});

 public void simple()
    {
        Response.Clear();
        Home h = new Home();
        //h.logout();

        Response.Write("some string");
        Response.End();
    }
    public void logout()
    {
        Response.Redirect(Config.Value("logout"));
    }

    <add key="logout" value="http://localhost:52232/Account/Social" />

when i call the server side method from client side it is returning the whole page not redirecting please help me Thank You

Upvotes: 0

Views: 483

Answers (2)

Arun Raj
Arun Raj

Reputation: 969

this Ajax call return a value from server side. If you want to redirect the page, you need to specify it.

$(document).ready(function () {
            $('#logout').on('click', function () {
                console.log("clicked");
                $.ajax({
                    type: "POST",
                    url: 'Home.aspx?method=simple'
                }).done(function (data) {
                    window.location = data;
                });

            });
        });

code behind

protected void Page_Load(object sender, EventArgs e)
{
  if (!String.IsNullOrEmpty(Request.QueryString["method"]))
  {
    if (Request.QueryString["method"].Equals("simple"))
    {
      Response.Write(ConfigurationManager.AppSettings["logout"]);
      Response.End();
    }
  }
}

Or use web method

[WebMethod]
public static string logout()
{
    return Config.Value("logout"));
}

then ajax url change to url: 'Home.aspx/logout'

Upvotes: 0

Keval Gangani
Keval Gangani

Reputation: 1328

Your logout method must be static.

[WebMethod]
public static void logout()
{
    Response.Redirect(Config.Value("logout"));
}

And call logout method instead of simple in jQuery AJAX call.

$('#logout').on('click', function () {
console.log("clicked");
$.ajax({
    type:"GET",
    url: 'Home.aspx/logout'
    }).done(function (data) {
    console.log(data);
});
//PageMethods.simple();
});

Another way is to use window.location to redirect end user in client side.

$('#logout').on('click', function () {
    window.location = "http://localhost:52232/Account/Social";
});

Upvotes: 1

Related Questions