Calling Web Services from the Client using jQuery
1. .ASMX page - Web Service Code:
2. .ASPX Page - Calling web service using jQuery
[WebService(Namespace
= http://tempuri.org/)]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be
called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class QuotationService : System.Web.Services.WebService
{
[WebMethod]
public string
GetQuote()
{
List<string>
quotes = new List<string>();
quotes.Add("The fool who is silent
passes for wise.");
quotes.Add("The early bird catches the
worm.");
quotes.Add("If wishes were true,
shepherds would be kings.");
Random rnd = new Random();
return quotes[rnd.Next(quotes.Count)];
}
}
DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Web Service Methodtitle>
<script type="text/javascript"
src="../Scripts/jquery-1.4.1.js">script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnGet").click(function () {
$.ajax({//makes an asynchronous call to a
URL.
type: "POST",
dataType: "json",
contentType: "application/json",
url: "QuotationService.asmx/GetQuote",
success: function (data) {
$("#spanQuote").html(data.d);
},
error: function
() {
alert("The call to the web service failed.");
}
})
});
});
script>
head>
<body>
<form id="form1" runat="server">
<div>
<input id="btnGet" type="button"
value="Get
Quote" />
<br />
<br />
<span id="spanQuote">span>
div>
form>
body>
html>
Comments
Post a Comment