Please run this in a new page
Ref:
Send (Pass) multiple parameters to WebMethod in jQuery AJAX POST in ASP.Net
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnSubmit]").click(function () {
var obj = {};
obj.name = $.trim($("[id*=txtName]").val());
obj.age = $.trim($("[id*=txtAge]").val());
var user = {};
user.City = $("[id*=txtCity]").val();
user.Country = $("[id*=txtCountry]").val();
obj["details"] = user;
$.ajax({
type: "POST",
url: "Default2.aspx/SendParameters",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
}
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text="Azim" />
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID="txtAge" runat="server" Text="24" />
</td>
</tr>
<tr>
<td>
City:
</td>
<td>
<asp:TextBox ID="txtCity" runat="server" Text="Mumbai" />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" Text="India" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
VB.Net
Public Class User
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = Value
End Set
End Property
Private m_name As String
Public Property age() As String
Get
Return m_age
End Get
Set(value As String)
m_age = Value
End Set
End Property
Private m_age As String
End Class
Public Class Details
Public Property City() As String
Get
Return m_City
End Get
Set(value As String)
m_City = Value
End Set
End Property
Private m_City As String
Public Property Country() As String
Get
Return m_Country
End Get
Set(value As String)
m_Country = Value
End Set
End Property
Private m_Country As String
End Class
<System.Web.Services.WebMethod()> _
Public Shared Function SendParameters(age As Integer, name As String, details As Details) As String
Return String.Format("Name: {0}{2}Age: {1} City: {3} {4} Country:{5}", name, age, Environment.NewLine, details.City, Environment.NewLine, _
details.Country)
End Function
C#
public class User
{
public string name { get; set; }
public string age{ get; set; }
}
public class Details
{
public string City { get; set; }
public string Country { get; set; }
}
[System.Web.Services.WebMethod]
public static string SendParameters(int age, string name, Details details)
{
return string.Format("Name: {0}{2}Age: {1} City: {3} {4} Country:{5}", name, age, Environment.NewLine, details.City,Environment.NewLine,details.Country);
}