Calling ASMX Web Services from Javascript using AJAX November 19, 2008
Posted by koolkatblog in ASP.NET.Tags: AJAX, web service
add a comment
This is an example of how you can call a web service from your javascript using AJAX.
1. To begin, create a new AJAX enabled website.
2. Add a class to your project. Call it Pizza.cs and add the following code:
public Pizza(string name, string desc, double cost)
{
this.m_name = name;
this.m_desc = desc;
this.m_cost = cost;
}
private string m_name;
private string m_desc;
private double m_cost; public string name { get { return this.m_name; }set { this.m_name = value; } }
public string desc { get { return this.m_desc; } set { this.m_desc = value; } }
public double cost { get { return this.m_cost; } set { this.m_cost = value; }
2. Add a web service to your project. Call it PizzaPlace.asmx and add the following 2 methods:
[WebMethod]
public int GetLocationCount(int zipCode)
{
int locations = 0;
switch (zipCode)
{
case 89128:
locations = 5;
break;
case 89118:
locations = 7;
break;
default:
break;
}
return locations;
}
[WebMethod]
public List<Pizza> GetDeals()
{
List<Pizza> pz = new List<Pizza>();
Pizza p1 = new Pizza(“Pepperoni Special”, “Large Pepperoni”, 9.99);
Pizza p2 = new Pizza(“Vegetarian”, “Large Mushroom, Onions, Peppers, Olives”, 10.99);
pz.Add(p1);
pz.Add(p2);
return pz;
}