10

こんにちは、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);
}
4

4 に答える 4

3

リダイレクト先のプロファイルを持つユーザーの ID を Web メソッドに返す必要があります。次に、jQuery 成功コールバックで、次のように、window.location をパスとクエリ文字列に設定します。

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) {
            // Redirect to personal_profile.aspx passing it the ID we got back from the web method call
            window.location = "personal_profile.aspx?id=" + res;
        },
        error: function (res, msg, code) {
            // log the error to the console
        } //error
    });
}

[WebMethod]
public static string redirect_to_profile()
{
    dbservices db=new dbservices();
    string user = HttpContext.Current.User.Identity.Name;
    return db.return_id_by_user(user);
}
于 2013-06-12T19:25:27.233 に答える
2

HttpResponse.Redirect を実行する代わりに、生成したこの URL を Javascript (ajax 呼び出しへの応答) に送信し、Javascript コードを使用してリダイレクトすることができます。

于 2013-06-12T19:18:20.400 に答える
0

これを試して:

function myFun(a) {
            var s = null;
            var em = document.getElementById(a + 'productno');
            if (em != null)
                PageMethods.setSession(em.innerText);
            window.location.assign("/Product%20Details.aspx");


        }
于 2014-12-20T00:49:21.577 に答える