【发布时间】:2013-06-09 00:42:34
【问题描述】:
您好,我正在尝试从 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
});
}
这里是a href:
<a onclick="redirect_to_profile()">Personal Profile</a>
这是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);
}
【问题讨论】:
-
1) 没有静态
Response。 2)无论如何,这不会是你想要的。您需要与 JS 交谈。 -
我试图和它说话,它不会回答 :) 你这是什么意思?
-
你需要返回一个告诉 JS 做什么的结果。
-
你为什么使用 AJAX? 听起来你想要链接点击导致浏览器加载不同的页面。如果您只想加载一个新页面,AJAX 是一个糟糕的选择。
标签: c# javascript asp.net ajax static-methods