mardi 3 avril 2018

Ajax Request return : data Udefined After Success

I am trying to do some Get_Data from dataBase Using asynchronis ajax request and WebMethod Everything works fine , but when i want the returned value from my web method value , i got undefined value .
I Know That this question is asked before I have tried almost all advices to achieve my wanted goal , but unfortunately nothing happened .

  1. Create Instance Outside the function than set it to the return value.
  2. Set The dataType:"Json"
  3. Create an instance inside the function and set it to empty string var ReturnValue="";
  4. Let Json object stringify my returned value

and many other steps.
So please do not mark my question as duplicated value

Here is my code below : //Javascript and Jquery Codes

            $(document).ready(function () {
                $("#PValidateLogin").hide();

            });

            function killSpaces(element) 
{
                var characters = element.toString();
                var resultText = "";
                for (var i = 0; i < characters.length; i++) {
                    if (!(characters[i] == " ")) {
                        resultText += characters[i];
                    }
                }
                return resultText;
            }


            $("#<%=btnLogin.ClientID%>").click(function () {
                var MyWantedhref;
                var Result = ValidateMyLogin();
                if (typeof Result === "undefined") {
                    alert("Something wrong with codes");
                }
                else if (Result == "false" || killSpaces(Result).length == 0)
                {
                    alert("wrong user ");
                }
                else
                {
                    MyWantedhref = "/Frm_Manager/frm_MenuCategory.aspx";
                    location.replace(MyWantedhref);
                }
            });


            function ValidateMyLogin()
            {
                var ReturValue;
                    var UserName = $("#<%=txtUserName.ClientID%>").val();
                    var Password = $("#<%=txtUserPassword.ClientID%>").val();
                    if (killSpaces(Password).length >= 4 && killSpaces(UserName).length >= 4)
                    {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json;charset=utf-8",
                            dataType:"json",
                            url: "frm_ValidateLogin.aspx/ValidateLogin",
                            data: "{'UserName':'" + UserName + "','Password':'" + Password + "'}",
                            success: function (data)
                            {                                
                                ReturValue = data.d;
                            },
                            error: function (err)
                            {
                                ReturValue = err.d;
                            }
                        });                      
                    }

                return ReturValue;
            }

And Here are the C# Code

<code>
 private static OwnerAccountBO _OwnerAccountBO;
        public static OwnerAccountBO OwnerAccountBO { get { return new OwnerAccountBO(); } set { _OwnerAccountBO = value; } }

        [WebMethod(EnableSession = true)]
        public static string ValidateLogin(string UserName, string Password)
        {
            string CharResult = "";
            object[] UserParameters = { UserName, Password };
            int Result = OwnerAccountBO.validateLogin(UserParameters);
            if (Result > 0)
            {
                HttpContext.Current.Session["OwnerAccountID"] = Result;
                CharResult += Convert.ToString(Result);
            }
            else
            {
                CharResult += "false";
            }
            return CharResult;
        }
</code>

Please Any Help would be thankful . And of course i want a proper explication for the wrong action or flow that i am doing to have such error




Aucun commentaire:

Enregistrer un commentaire