2015-05-04 62 views
0
$('#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" /> 

当我打电话从客户端服务器端方法,它返回整个页面没有进行重定向,请帮我 谢谢的Response.Redirect不是重定向页面

回答

1

你注销方法必须是静态的。

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

并调用logout方法,而不是simple jQuery的AJAX调用。

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

的另一种方法是使用window.location到最终用户重定向在客户端。

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

这个Ajax调用从服务器端返回一个值。如果你想重定向页面,你需要指定它。

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

      }); 
     }); 

代码背后

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(); 
    } 
    } 
} 

或者使用Web方法

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

然后一个AJAX网址变更网址: 'Home.aspx /注销'