So easy, JQuery calls WebServices

Source: Internet
Author: User

It has been a long time since the previous blog article was written, so it is not easy to learn, and there is no time. Haha, not much nonsense.
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? Method = getequalmetn & param1 = 1 & param2 = 2 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:
1. A call with no parameters or return values
Front-end JS

$ ("# 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

[WebMethod]public string HelloWorld(){      return "Hello World";}

Google debugging result:

2 simple parameter simple return value call
Front-end JS

$ ("# 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

[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

$ ("# 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

[WebMethod] public List <Student> GetStudentList (Student stu) {List <Student> studentList = new List <Student> {new Student {ID = 1, Name = "Zhang San "}, new Student {ID = 2, Name = ""}; // puts the entity from the client back to the returned value studentList. add (stu); return studentList ;}

Google debugging result:

4. Return the WebMethod call of an anonymous object
Front-end JS

$ ("# 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

 

[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! I still don't know how to upload the source code. I need to add QQ: 312117511 or leave a mailbox

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.