JQuery calls methods and four instances of WebServices

  • 2020-03-30 02:51:38
  • OfStack

You even add a backend page for each ajax request!
Are you even thinking, nima, how great it would be to be able to call methods directly in C# class files? ! (here, FishLi has created a framework for you to check out.)
But, you probably forget, we are programmers, we are lazy, we want the computer to do more things for us! But actually, Microsoft and JQuery have solved this little problem for us.

The general calls are divided into the following:

A, Calls with no arguments and return values

Front-end JS code:


$("#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 wrong " + error.responseText);
                    }
                });
            });

Back-end WebMethod code:


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


Debugging results with Google:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/20145684647714.png? 2014468470 ">


Simple parameter simple return value of the call

Front-end JS code:


$("#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 wrong " + error.responseText);
                    }
                });
            });


Back-end WebMethod code:


[WebMethod]
        public string SimpleReturns(string name)
        {
            return String.Format(" Your name is {0}", name);
        }


Debugging results with Google:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/20145684732068.png? 20144684743 ">
Three,   Complex parameter complex return value call
Front-end JS code:

$("#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 wrong " + error.responseText);
                    }
                });
            });

The back-end the 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=" Li si "}
            };
            //Put the entity from the client back into the return value
            studentList.Add(stu);
            return studentList;
        }

Debugging results with Google:


Related articles: