JQuery calls the WebServices method and 4 instances _ jquery

Source: Internet
Author: User
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!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.