Ajax成功事件不会触发

时间:2019-09-26 13:39:00

标签: javascript c# ajax webmethod okta

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

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

如果我从ajax方法中删除contentType和dataType,则成功事件有效,但随后我无法对该方法进行调试,并且它没有执行应做的事情

我的目标是在Web方法的结尾处​​重定向到SignedIn.aspx,并尝试使用此代码,但也无法使其正常工作,这就是为什么我通过ajax成功方法在客户端进行操作

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

Ajax:

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

    [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));

    }

1 个答案:

答案 0 :(得分:0)

$('#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;

    }