Home >Web Front-end >JS Tutorial >jQuery calls WebService implementation code_jquery

jQuery calls WebService implementation code_jquery

WBOY
WBOYOriginal
2016-05-16 18:06:001140browse

An example:
1. In .aspx:

Copy code The code is as follows:

2. In WebService:
Copy code The code is as follows:

[WebService( Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//To allow this web service to be called from scripts using ASP.NET AJAX, cancel Comments on the following line.
[System.Web.Script.Services.ScriptService] //If you want to use jquery to call WebService, uncomment the previous one
public class WebService : System.Web.Services.WebService
{
public WebService ()
{
//If using designed components, uncomment the following lines
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string .Format("Wish you {0}, {1}, {2} in {3} years", value1, value2, value3, value4);
}
[WebMethod]
public List< int> 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 = "Good luck in the Year of the Ox";
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"] = "Happy New Year";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = " 2";
dr["Value"] = "All the best";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
//Customized class, only two attributes
public class Class1
{
public string ID { get; set; }
public string Value { get ; set; }
}

3. In JS:
Copy code The code is as follows :



4. Effect
jQuery calls WebService implementation code_jquery
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn