2015-09-19

jQuery Call Web Service using Ajax - ASP.NET asmx

1. Create a folder, call webService, in your project, and create myService.asmx in this folder
2. and create a html file student.html.
add studentList method to myService.asmx.cs namespace MyWebTest { public class myService : System.Web.Services.WebService { [WebMethod] public List studentList() { List list = new List (); list.Add ("John"); list.Add ("David"); list.Add ("Kelly"); return list; } } } In student.html, use ajax to call web service studentList $(document).ready(function () { $.ajax({ type: "POST", url: "../webService/myService.asmx/studentList", data: "", dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { var studs = data.d $.each(studs, function (idx, stud){ $("#list").append(stud); }); } }); });

No comments: