Do you often perform this kind of development? The front end uses JS to write logic, and the back end uses aspx or ashx as the service? Do you often splice a. aspx in the query string when requesting aspx? MethodgetDepartmetn & amp; param11 & amp; param22 string? You even add a backend page for each ajax request!
Are you even thinking, Nima, if you can directly call the methods in Class C # files, it will be nice ?! (Here, FishLi provides a framework. If you are interested, you can check it out)
However, you probably forgot that we are programmers and we are lazy. We want computers to do more things for us! (Install 13 here), but in fact, Microsoft and JQuery have helped us solve this small problem.
There are roughly the following types of calls:
I,Calls with no parameters or return values
Front-end JS Code:
The Code is as follows:
$ ("# Btn1"). click (function (){
$. Ajax ({
Type: "POST ",
ContentType: "application/json; charset = UTF-8 ",
Url: "CalledByJquery. asmx/HelloWorld ",
Data :"{}",
DataType: "json ",
Success: function (json) {alert (json. d );},
Error: function (error ){
Alert ("call error" + error. responseText );
}
});
});
Backend WebMethod code:
The Code is as follows:
[WebMethod]
Public string HelloWorld ()
{
Return "Hello World ";
}
Google debugging result:
Ii. Simple parameter simple return value call
Front-end JS Code:
The Code is as follows:
$ ("# Btn2"). click (function (){
$. Ajax ({
Type: "POST ",
ContentType: "application/json; charset = UTF-8 ",
Url: "CalledByJquery. asmx/SimpleReturns ",
Data: "{name: 'zhang san '}",
DataType: "json ",
Success: function (json) {alert (json. d );},
Error: function (error ){
Alert ("call error" + error. responseText );
}
});
});
Backend WebMethod code:
The Code is as follows:
[WebMethod]
Public string SimpleReturns (string name)
{
Return String. Format ("Your name is {0}", name );
}
Google debugging result:
3. Call complex return values of complex parameters
Front-end JS Code:
The Code is as follows:
$ ("# Btn3"). click (function (){
$. Ajax ({
Type: "POST ",
ContentType: "application/json; charset = UTF-8 ",
Url: "CalledByJquery. asmx/GetStudentList ",
Data: "{stu: {ID: '6', Name: 'ff '}}",
DataType: "json ",
Success: function (json) {alert (json. d );},
Error: function (error ){
Alert ("call error" + error. responseText );
}
});
});
Backend WebMethod:
The Code is as follows:
[WebMethod]
Public List GetStudentList (Student stu)
{
List StudentList = new List
{
New Student {ID = 1, Name = "James "},
New Student {ID = 2, Name = ""}
};
// Put the entity from the client back into the return value
StudentList. Add (stu );
Return studentList;
}
Google debugging result:
Iv. WebMethod call for returning anonymous objects
Front-end JS Code:
The Code is as follows:
$ ("# Btn4"). click (function (){
$. Ajax ({
Type: "POST ",
ContentType: "application/json; charset = UTF-8 ",
Url: "CalledByJquery. asmx/ReturnNoNameClass ",
Data :"{}",
DataType: "json ",
Success: function (json) {alert (json. d );},
Error: function (error ){
Alert ("call error" + error. responseText );
}
});
});
Backend WebMethod code:
The Code is as follows:
[WebMethod]
Public object ReturnNoNameClass ()
{
Return new {ID = 1, Name = "James "};
}
Google debugging result:
Haha, do you think so easy here? Mom no longer needs to worry about my learning. In fact, many things are very simple, but no one tells us, however, we do not have this kind of requirement in actual development, which creates some obstacles for our development,
So how important is communication!