【问题标题】:Ajax success event doesn't fireAjax 成功事件不会触发
【发布时间】:2025-12-08 16:45:01
【问题描述】:

在 WebForms 上进行 Okta 身份验证 登录有效,但重定向部分无效

我尝试使用 void 并返回 json 对象/字符串,但不起作用

如果我从 ajax 方法中删除 contentType 和 dataType,成功事件会起作用,但是我无法调试该方法,并且它没有做预期做的事情

我的目标是在 webmethod 的末尾重定向到 SignedIn.aspx 尝试使用此代码但无法使其工作,这就是我通过 ajax 成功方法做客户端的原因

     HttpContext.Current.Response.Redirect("SignedIn.aspx");

阿贾克斯:

function FormSubmit() {
    $.ajax({
        type: "POST",
        url: "Example.aspx/Login",
        data: "{hiddenSessionTokenField:'" + $('#hiddenSessionTokenField').val() + "'}",
        dataType: "json",
        async:false,
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            alert("Method Called Sucessfully" + response);
            window.location.href = "http://localhost:8080/SignedIn.aspx";
        },
        error: function (response) {
            alert("error " + response);
        }
    });
}

网络方法

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static void Login(string hiddenSessionTokenField)
        {

        //var result = new { url = "http://localhost:8080/SignedIn.aspx" };

        if (!HttpContext.Current.User.Identity.IsAuthenticated)
        {
            var properties = new AuthenticationProperties();
            properties.Dictionary.Add("sessionToken", hiddenSessionTokenField);
            properties.RedirectUri = "~/SignedIn.aspx";

            //Okta Authentication
            HttpContext.Current.GetOwinContext().Authentication.Challenge(properties,
                OpenIdConnectAuthenticationDefaults.AuthenticationType);


            //System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

            //return s.Serialize(result));

        }
            //return s.Serialize(result));

    }

【问题讨论】:

    标签: javascript c# ajax webmethod okta


    【解决方案1】:
    $('#test').on('click', function () {
        $.ajax({
          type: "POST",
          url: "TEST.aspx/Login",
          data: "{hiddenSessionTokenField:'" + $('#hiddenSessionTokenField').val() + "'}",
          dataType: "json",
          contentType: "application/json; charset=utf-8",
          success: function (response) {
        //    alert("Method Called Sucessfully");
          window.location.href = "http://localhost:8080/index.aspx";
       },
        error: function (response) {
          alert("error " + response);
           }
          });
         })
    
    
        public static void Login(string hiddenSessionTokenField) {
            int x = 0;
    
        }
    

    【讨论】:

    • 就像我说的那样,我更具体地尝试了这个 HttpContext.Current.Response.Redirect("SignedIn.aspx");因为方法是静态的,我必须添加 HttpContext.Current 但它没有做任何事情
    • 不确定是不是拼写错误,但应该是 Response.Redirect("~/SignedIn.aspx");
    • 我确实尝试过使用 "~/SignedIn.aspx" 但就像我说的那样它不起作用
    • 用一个适合我的例子编辑了我的答案。
    • 我需要在静态 WebMethod 中使用它
    最近更新 更多