Recently, a friend around me was working on a project, and one of the module requirements required using js to call webService. He asked me to help him think of a solution, because I had never done this before. After about an hour of testing, it worked. I found this stuff to be so simple after I wrote it down. At first I thought it was complicated. Share the code written during testing, I hope it will be helpful to everyone.
WebService file content
code
using System;using System.Web;using System.Collections;using System.Collections.Generic;using System.Web.Services;using System.Web.Services.Protocols;using System.Web.UI.MobileControls;/// <summary >/// Summary description of JsWebService/// </summary>[WebService(Namespace = " http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services .ScriptService] public class JsWebService : System.Web.Services.WebService{ public JsWebService() { //If using designed components, please uncomment the following line //InitializeComponent(); } [WebMethod] public string GetString() { return "GetString"; } [WebMethod] public string GetString(string name) { return string.Format("{0}Welcome!", name); } [WebMethod] public List<string> GetList() { List< string> listStr = new List<string>(); for (int i = 0; i < 10; i++) { listStr.Add("test" + i); } return listStr; } [WebMethod] public List<JsWebServiceObject> GetObjectList() { List<JsWebServiceObject> objs = new List<JsWebServiceObject>(); for (int i = 0; i < 10; i++) { JsWebServiceObject obj = new JsWebServiceObject("name" + i, i + 20); objs .Add(obj); } return objs; }}
The aspx page code that calls WebService
code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns=" http://www.w3.org/1999/xhtml"><head runat="server"> < title>Js calls WebService</title> <script type="text/javascript"> function GetString() { JsWebService.GetString(GetStringCallBack); //Call the method in WebService and set the callback function} function GetStringCallBack(result) { alert(result); } function GetStringParam(name) { JsWebService.GetString(name,GetStringParamCallBack); //Call the method in WebService and set the callback function. If there are parameters in this method, add this parameter before the callback function. } function GetStringParamCallBack(result) { alert(result); } function GetList() { JsWebService.GetList(GetListCallBack); } function GetListCallBack(result) { if(result.length!=0) { for(var i=0;i <result.length;i++) { document.getElementById("contentDivGetList").innerHTML += result[i] + "<br/>"; } } } function GetObjectList() { JsWebService.GetObjectList(GetObjectListCallBack); } function GetObjectListCallBack (result) { if(result.length!=0) { for(var i=0;i<result.length;i++) { document.getElementById("contentDivGetObjectList").innerHTML += "Name:" + result[i ].Name + "Age:" + result[i].Age + "<br/>"; } } } </script></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="smJs" runat="server" > <Services> <asp:ServiceReference Path="WebService/JsWebService.asmx" /> </Services> </asp:ScriptManager> <div> <input type= "button" name="btnJsWebService" value="GetString" onclick="GetString()" /> <input type="button" name="butJsWebService" value="GetList" onclick="GetList()" /> <input type="button" name="butJsWebService" value="GetObjectList" onclick="GetObjectList()" /> <input type="button" name="butJsWebService" value="GetStringParam" onclick="GetStringParam('js calls WebService ')" /> <div id="contentDivGetList"></div> <div id="contentDivGetObjectList"></div> </div> </form></body></html