Hi nauna,
Refer below sample.
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/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 Validate() {
var obj = {};
obj.name = document.getElementById("txtName").value;
obj.price = document.getElementById("txtPrice").value;
$.ajax({
url: '<%=ResolveUrl("~/WebService.asmx/GetCustomers") %>',
data: JSON.stringify(obj),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (r) {
if (obj.price % 1 != 0) {
alert(r.d);
}
else {
alert("Enter Price in decimal");
}
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
</script>
<div>
Name :<asp:TextBox runat="server" ID="txtName" />
<br />
Price :<asp:TextBox runat="server" ID="txtPrice" />
<br />
<asp:Button Text="Submit" runat="server" OnClientClick="Validate()" />
</div>
Code
C#
WebService.cs
using System.Web.Services;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCustomers(string name, decimal price)
{
return name + "\n" + price;
}
}
VB.Net
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebServiceVB
Inherits System.Web.Services.WebService
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Function GetCustomers(ByVal name As String, ByVal price As Decimal) As String
Return name & vbLf & price
End Function
End Class
Screenshot