I have created a asmx web service as follows:
[ScriptService]
public class CurrencyData : System.Web.Services.WebService
{
[WebMethod]
public string DisplayCurrency(double currency, string symbol ,string format)
{
switch(format)
{
case "short": symbol = "EUR";
break;
case "long": symbol = "EURO";
break;
case "simple":
break;
}
return currency.ToString() + symbol;
}
}
On the client side I have created an external js script to call above service containing the following code:
$(document).ready(function displaycurrency(monval, sym, fmt) {
var output;
$.ajax({
type: "POST",
url: "http://localhost:60413/CurrencyData.asmx/DisplayCurrency",
data: "currency=" + monval + "&symbol=" + sym + "&format=" + fmt + "", //posting the required currency & symbol
dataType: "text",
success: function (data) {
var xmlout = $.parseXML(data);
output = xmlout.childNodes[0].innerHTML; //getting the currency value from xml
}
});
return output;
});
I am using the the defined function to be called in an html page to display the currency value in between script tags:
<p id="#price" ><script>displaycurrency("22.56", "$", "long")</script></p>
<script type="text/javascript" src="~/Scripts/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="~/Scripts/CurrencyHLPR.js">/script>
//external js
But when I am trying to run the code it is giving "displaycurrency is not defined" error. I intend to used this method in multiple places in the html to print different currency formats. I am not very familiar in consuming web services through scripts/ajax, so not sure of other approach. Please give a solution.
Aucun commentaire:
Enregistrer un commentaire