Search code examples
javascriptc#ajaxwebmethodokta

Ajax success event doesn't fire


Doing Okta Authentication on WebForms The login works but the redirect part doesnt

I tried with void and to return json object/string but doenst work

if i delete contentType and dataType from ajax method the success event works but then i can't debbug the method and it wasn't doing what was suppose to do

My objective is at the end of the webmethod to redirect to SignedIn.aspx tried with this code but couldn't make it work either that's why im doing client side through ajax success method

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

    }

Solution

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