当前位置:Gxlcms > JavaScript > jQuery调用WebService的实现代码_jquery

jQuery调用WebService的实现代码_jquery

时间:2021-07-01 10:21:17 帮助过:17人阅读

一个例子说尽:
1、.aspx中:
代码如下:

HelloWorld

传入参数

返回集合

返回复合类型

返回DataSet(XML)

服务器处理中,请稍后



2、WebService中:
代码如下:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService] //如果要用jquery调用WebService则取消前面的注释
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);
}
[WebMethod]
public List GetArray(int i)
{
List list = new List();
while (i >= 0)
{
list.Add(i--);
}
return list;
}
[WebMethod]
public Class1 GetClass()
{
Class1 a = new Class1();
a.ID = "1";
a.Value = "牛年大吉";
return a;
}
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Table1");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Value", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["ID"] = "1";
dr["Value"] = "新年快乐";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "2";
dr["Value"] = "万事如意";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
//自定义的类,只有两个属性
public class Class1
{
public string ID { get; set; }
public string Value { get; set; }
}

3、JS中:
代码如下: