您好我想从一个aspx页面运行阿贾克斯一个WebMethod。基本上我想重定向到一个查询字符串在另一个aspx页面,但我想从&LT做到这一点; A HREF>
,怎么一回事,因为它是一个jQuery菜单的一部分
这是我学到了什么,我只能使用AJAX调用静态webMethods的,但我canot重定向从我的静态函数。
视觉工作室,标志着它在一个红线说:的对象引用是必需的非静态字段的方法或属性System.Web.Htt presponse.Redirect(串)
这里是Ajax调用:
函数redirect_to_profile(){
$阿贾克斯({
键入:POST,
网址:personal_profile.aspx.cs.aspx / redirect_to_profile
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
成功:函数(RES){
警报(成功);
},
错误:函数(RES,味精,code){
//错误记录到控制台
} //错误
});
}
这里是A HREF:
<一的onclick =redirect_to_profile()>个人简介< / A>
这里是里面的WebMethod的personal_profile.aspx
[WebMethod的]
公共静态无效redirect_to_profile()
{
dbservices DB =新dbservices();
字符串用户= HttpContext.Current.User.Identity.Name;
字符串ID = db.return_id_by_user(用户);
HTT presponse.Redirect(personal_profile.aspx ID =?+ ID);
}
解决方案
您需要将构造URL返回给客户端:
公共静态字符串redirect_to_profile()
{
dbservices DB =新dbservices();
字符串用户= HttpContext.Current.User.Identity.Name;
字符串ID = db.return_id_by_user(用户);
返回personal_profile.aspx ID =?+ ID;
}
然后使用JavaScript,在成功
AJAX调用的功能,设置位置:
了window.location =资源;
也许
了window.location = res.d;
Hello I'm trying to run a webmethod with ajax from an aspx page. basically I want to redirect to another aspx page with a query string, but I want to do it from <a href>
, beacuse it's part of a jquery menu.
from what I learned I can only use ajax to call static webmethods, but I canot redirect from my static function.
visual studio marks it in a red line saying: "an object reference is required for the nonstatic field method or property System.Web.HttpResponse.Redirect(string)"
here is the ajax call:
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
});
}
here is the a href:
<a onclick="redirect_to_profile()">Personal Profile</a>
here is the webmethod inside the personal_profile.aspx
[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);
}
解决方案
You will need to return the constructed URL to the client:
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;
}
Then using JavaScript, in the success
function of the AJAX call, set the location:
window.location = res;
Or perhaps:
window.location = res.d;