最近身邊的朋友做項目,其中有一個模組需求要求使用js去呼叫webService,讓我幫忙招,因為以前卻實沒有這麼做。經過大約一個小時的測試,成功了。發現這個東東寫出來之後是這麼簡單。當初是我想複雜了。分享一下測試時寫的程式碼,希望對大家有幫助。
WebService文件內容
程式碼
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 >/// 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() { //如果使用設計的元件,請取消註解以下行//InitializeComponent(); } [WebMethod] public string GetString() { return "GetString"; } [WebMethod] public string GetString(string name) { return string.Format("{0}歡迎您的到來!", 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; }}
呼叫WebService的aspx頁面程式碼
程式碼
<%@ 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呼叫WebService</title> <script type="text/javascript"> function GetString() { JsWebService.GetString(GetStringCallBack); //在WebService中呼叫方法,並設定回呼函數} function GetStringCallBack(result) { alert(result); } function GetStringParam(name) { JsWebService.GetString(name,GetStringParamCallBack); //在WebService中呼叫方法,並設定回呼函數,如果此方法中帶有參數則在回呼函數前加入此參數。 } function GetStringParamCallBack(result) { alert(result); } function GetList() { JsWebService.GetList(GetListCallBack); } function GetListCallBack(result) { if(result.length!=0) { for(var i=0; <result.length;i++) { document.getElementById("contentDivGetList").innerHTML += result[i] + "<br/>"; } } function GetObjectList() { JsWebService.GetObjectList(GetObjectListCallBack); (result) { if(result.length!=0) { for(var i=0;i<result.length;i++) { document.getElementById("contentDivGetObjectList").innerHTML += "姓名:" + result[i ].Name + " 年齡:" + 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()" /> <inputService" value="GetList" onclick="GetList()" /> <inputService" type="button" name="butJsWebService" value="GetObjectList" onclick="GetObjectList()" /> <input type="button" name="butJsWebService" value="GetStringParam" onclick="GetStringParam('jsWebService" value="GetStringParam" onclick="GetStringParam('jsWebService ')" /> <div id="contentDivGetList"></div> <div id="contentDivGetObjectList"></div> </div> </form></body></html