Passing a JSON data object from jquery ajax to asp.net webmethod

I have data similar to this:

var data = { email: $("#txtEmail").val(), password: $("#txtPassword").val() } data = JSON.stringify(data); 

I use jquery ajax and pass this data to my web method. This all works if my web method looks like this:

 [WebMethod] public static Response TryLogin(string email, string password) {..} 

But I'm trying to pass data to a web method that looks like this:

 [WebMethod] public static Response TryLogin(LoginData data) {..} 

My LoginData class looks something like this:

 public class LoginData { public string email { get; set; } public string password { get; set; } } 

When I try to do this, I get the following error:

error: 500: {"Message": "Invalid web service call, missing value for parameter: \ u0027data \ u0027.

How to do it right?

+4
source share
1 answer
 data = JSON.stringify({data: data}); 

To clarify, you are currently sending 2 parameters, while your web method expects only one (named data).

+4
source

All Articles