jquery invokes the WebServices method and 4 instances _jquery

Source: Internet
Author: User

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!

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.