JS技术

使用Js调用WebService_Javascript教程

字号+ 作者:H5之家 来源:H5之家 2015-10-02 12:07 我要评论( )

使用Js调用WebService,学习使用Js调用WebService,使用Js调用WebService,查看使用Js调用WebService,WebService文件内容 代码 using System;using System.Web;usin

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 = "")][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" ""><html xmlns=""><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;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 += "姓名:" + 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()" />            <input type="button" name="butJsWebService" value="GetObjectList" onclick="GetObjectList()" />            <input type="button" name="butJsWebService" value="GetStringParam" onclick="GetStringParam('js调用WebService')" />                        <div id="contentDivGetList"></div>            <div id="contentDivGetObjectList"></div>                    </div>    </form></body></html>
-

资料引用:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评