Dvi*_*ski 10 javascript c# asp.net ajax static-methods
您好我正在尝试使用aspx页面中的ajax运行webmethod.基本上我想用一个查询字符串重定向到另一个aspx页面,但我想这样做<a href>,因为它是jquery菜单的一部分.
根据我的学习,我只能使用ajax来调用静态webmethods,但我可以从我的静态函数中重定向.
visual studio用红线标记它:"非静态字段方法或属性System.Web.HttpResponse.Redirect(string)需要一个对象引用"
这是ajax调用:
function redirect_to_profile() {
$.ajax({
type: "POST",
url: "personal_profile.aspx.cs.aspx/redirect_to_profile",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
alert("success");
},
error: function (res, msg, code) {
// log the error to the console
} //error
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个href:
<a onclick="redirect_to_profile()">Personal Profile</a>
Run Code Online (Sandbox Code Playgroud)
这是personal_profile.aspx中的webmethod
[WebMethod]
public static void redirect_to_profile()
{
dbservices db=new dbservices();
string user = HttpContext.Current.User.Identity.Name;
string id = db.return_id_by_user(user);
HttpResponse.Redirect("personal_profile.aspx?id="+id);
}
Run Code Online (Sandbox Code Playgroud)
Gra*_*mas 21
您需要将构造的URL返回给客户端:
public static string redirect_to_profile()
{
dbservices db=new dbservices();
string user = HttpContext.Current.User.Identity.Name;
string id = db.return_id_by_user(user);
return "personal_profile.aspx?id="+id;
}
Run Code Online (Sandbox Code Playgroud)
然后使用JavaScript,在successAJAX调用的函数中,设置位置:
window.location = res;
Run Code Online (Sandbox Code Playgroud)
也许:
window.location = res.d;
Run Code Online (Sandbox Code Playgroud)