こんにちは、aspx ページから ajax を使用して webmethod を実行しようとしています。<a href>
基本的に、クエリ文字列を使用して別のaspxページにリダイレクトしたいのですが、jqueryメニューの一部であるため、からやりたいです。
私が学んだことから、ajaxを使用して静的Webメソッドを呼び出すことしかできませんが、静的関数からリダイレクトすることはできません。
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);
}