You even add a back-end page for each AJAX request!
Do you even think, Ni-ma, if you can directly call the C # class file in the method is cool?! (Here Fishli made a frame, interested to see)
However, you probably forget, we are programmers, we are lazy, we have to let the computer to do more things for us! (loading 13 here), but in fact, Microsoft and jquery Daniel have helped us solve this small problem.
The approximate calls fall into the following categories:
One, call with no parameter to return value
Front-end JS code:
Copy Code code 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);
}
});
});
Back-end WebMethod code:
Copy Code code as follows:
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
With the results of Google debugging:
second, simple parameter simple return value of the call
Front-end JS code:
Copy Code code as follows:
$ ("#btn2"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json; Charset=utf-8 ",
URL: "Calledbyjquery.asmx/simplereturns",
Data: "{name: ' John '}",
DataType: "JSON",
Success:function (JSON) {alert (JSON.D);},
Error:function (Error) {
Alert ("Call error" + Error.responsetext);
}
});
});
Back-end WebMethod code:
Copy Code code as follows:
[WebMethod]
public string Simplereturns (string name)
{
Return String.Format ("Your name is {0}", name);
}
With the results of Google debugging:
Call of complex parameter return value
Front-end JS code:
Copy Code code 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);
}
});
});
Back-end WebMethod:
Copy Code code as follows:
[WebMethod]
Public list<student> getstudentlist (Student Stu)
{
list<student> studentlist = new list<student>
{
New Student{id=1,name= "John"},
New student{id=2,name= "Dick"}
};
Put the entity that came from the client back into the return value
Studentlist.add (Stu);
return studentlist;
}
With the results of Google debugging:
Returns the WebMethod call of an anonymous object
Front-end JS code:
Copy Code code 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);
}
});
});
Back-end WebMethod code:
Copy Code code as follows:
[WebMethod]
public Object Returnnonameclass ()
{
return new {ID = 1, Name = "John"};
}
With the results of Google debugging:
Haha, here, you are not also feel so easy, mother no longer worry about my study, in fact, a lot of things are very simple, but no one told us, and we have not in the actual development of this demand, so to our development caused a certain obstacle,
So, communication ah, is how important!