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