1, write 4 kinds of WebService methods
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ScriptService]//enable WebService to successfully pass in the JSON parameter and return the result in JSON form
[Generatescripttype (typeof (person)]///Not necessary but recommended (if the person is nested within another complex type, it is necessary to declare)
[ToolboxItem (False)]
public class WebService1:System.Web.Services.WebService
{
///
Without any parameters
///
///
[WebMethod]
public string HelloWorld ()
{
Return "Hello World";
}
///
Incoming parameters
///
///
///
[WebMethod]
public string Hello (string name)
{
return string. Format ("Hello {0}", name);
}
///
Returns a generic list
///
///
///
[WebMethod]
Public list<int> createarray (int i)
{
list<int> list = new list<int> ();
while (i >= 0)
{
List. ADD (i--);
}
return list;
}
///
Return Complex Type
///
///
///
///
[WebMethod]
Public person Getperson (string name, int age)
{
return new Person ()
{
Name = name,
Age = Age
};
}
}
///
Complex types
///
public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}
2, write JS call the above method
<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Webapplication1.webform1"% >
DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page title>
<style type= "Text/css" >
Input
{
width:200px;
}
<style>
<script type= "Text/javascript" src= "Jquery-1[1].2.6.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
/*
1. The URL for WebService request type is Post,webservice "[Webserviceurl]/[webmethod]"
2. ContentType Declaration as JSON
3. Data to be passed in JSON string format
4. After setting the datatype as JSON, result is directly the JSON object returned.
*/
Calling the parameterless method
$ ("#btnHelloWorld"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webservice1.asmx/helloworld",
Data: "{}",
DataType: ' JSON ',
Success:function (Result) {
alert (RESULT.D);
}
});
});
1 parameters passed in
$ ("#btnHello"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webservice1.asmx/hello",
Data: "{name: ' Kimogigi '}",
DataType: ' JSON ',
Success:function (Result) {
alert (RESULT.D);
}
});
});
Returns a generic list
$ ("#btnArray"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webservice1.asmx/createarray",
Data: "{i:10}",
DataType: ' JSON ',
Success:function (Result) {
Alert (Result.d.join ("|"));
}
});
});
Return Complex Type
$ ("#btnPerson"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webservice1.asmx/getperson",
Data: "{name: ' Kimogigi ', age:26}",
DataType: ' JSON ',
Success:function (Result) {
var person = RESULT.D;
var showtext = [];
For (var p in person) {
Showtext.push (P + ":" + person[p]);
}
Alert (Showtext.join ("\ r \ n"));
}
});
});
});
</script>
<body>
<form id= "Form1" runat= "Server" >
<p>
<input type= "button" id= "Btnhelloworld" value= "HelloWorld"/>
<p>
<p>
<input type= "button" id= "Btnhello" value= "Hello"/>
<p>
<p>
<input type= "button" id= "Btnarray" value= "Createarray"/>
<p>
<p>
<input type= "button" id= "Btnperson" value= "Getperson"/>
<p>
<form>
<body>