0

1- I have collect The Login and Password from HTML Form with a javascript fonction

2- Then Call a C# webmethod with the javascript function to send the login and the password to the server

3- The server collect the data and should return a json object but when i try to display the result of the return json, nothing happing. None value is return.

Please help

This is the javascript code and the HTML Code on client side

    <script>
function OnRequestComplete(result, userContext, methodName) {
    var Person = eval(`enter code here`'(' + result + ')');
    alert(Person.Login);
    alert(Person.Password);
}
function OnRequestError(error, userContext, methodName) {
    if (error != null) {
    alert(error.get_message());
    }
}
function SubmitData() {
    var Login = document.getElementById("Login").value;
    var Password = document.getElementById("Password").value;
    PageMethods.GetData(Login, Password, OnRequestComplete, OnRequestError);
} 
</script>

This is the code on server side

  public partial class login : System.Web.UI.Page
{
    private static string SerializeObjectIntoJson(Person p)

    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, p);
            ms.Flush();
            byte[] bytes = ms.GetBuffer();
            string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
            return jsonString;
        }

    }
    [WebMethod]
    public static string GetData(string loginParam, string passwordParam)

    {
        Person p = new Person();
        p.Password = passwordParam;
        p.Login = loginParam;
        // throw new Exception("Custom Error :) ");
        return SerializeObjectIntoJson(p);
    }

} The person class

 public class Person
{
    string loginParam = string.Empty;
    string passwordParam = string.Empty;
    public string Password
    {
        get { return passwordParam; }
        set { passwordParam = value; }
    }
    public string Login
    {
        get { return loginParam; }
        set { loginParam = value; }
    }
}
OlsonBao
  • 1
  • 2
  • probably this can help: https://stackoverflow.com/questions/6928533/calling-a-webmethod-with-jquery-in-asp-net-webforms – felix-b Aug 04 '19 at 08:27

2 Answers2

0

First off, please format the bottom part please? It's hard to read.

my guess is that something is going wrong in SerializeObjectIntoJson(), if you want to transform a class object into JSON, microsoft advices to use Json.NET, then it would do the following:

private static string SerializeObjectIntoJson(Person p) {
    return JsonConvert.SerializeObject(p);
}

You can add Json.net to your project by using the Nuget Package Manager.

AlexSNorth
  • 153
  • 2
  • 10
0

The web service GetData is corretly returning the seriablize object in this form.

{"Login":"aaaaadd","Password":"ccc"}. I can capture this string. But when it come to call the javascript function OnRequestComplete get back this result on client side, i can't getany data when i call the function

   function OnRequestComplete(result, userContext, methodName) {
    var Person = eval('(' + result + ')');
    alert(Person.Login);
    alert(Person.Password);
}
OlsonBao
  • 1
  • 2