H_J_H

------专用杂物房
posts - 26, comments - 1, trackbacks - 0, articles - 119
除了传递和返回简单的类型,如字符串和整数型给web 服务,你也可以传递和返回复杂类型例如你的Web服务类。

下面是asmx的代码:

<%@ WebService Language="C#" Class="Quickstart.Samples.ComplexService" %>

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using Quickstart.Samples;//注意这里要using

namespace Quickstart.Samples
{
    public class Animal
    {
        String _name;
        String _color;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public String Color
        {
            get { return _color; }
            set { _color = value; }
        }
    };    
    
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ComplexService : System.Web.Services.WebService
    {
        [WebMethod]
        public Animal EchoAnimal(Animal a)
        {
            return a;//这里传递和返回的都是个Animal对象
        }
    }

}











下面是aspx页面的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Complex Type Service</title>

    <atlas:Script ID="Script1" runat="server" Path="~/ScriptLibrary/AtlasCompat.js" Browser="Mozilla" />
    <atlas:Script ID="Script2" runat="server" Path="~/ScriptLibrary/AtlasCompat.js" Browser="Firefox" />
    <atlas:Script ID="Script3" runat="server" Path="~/ScriptLibrary/Atlas.js"/>

    <!-- get script proxy -->
    <script type="text/javascript" language="JavaScript" src="ComplexService.asmx/js">
    </script>
</head>
<body>
    <h3>
        Name:<input id="inputName" />
        Color:<input id="inputColor" />
        <button id="buttonGo" onclick="return OnbuttonGo_click()">GO</button>
    </h3>

    <script type="text/javascript" language="JavaScript">
    function OnbuttonGo_click() 
    {
        //Call script proxy passing the input element data

        var i1 = document.getElementById('inputName');
        var i2 = document.getElementById('inputColor');
        //先new一个web 服务中的Animal对象
        var object = new Quickstart.Samples.Animal();

//给这个对象的属性赋值 object.Name = i1.value; object.Color = i2.value. //传递的是一个Animal对象 requestComplexService = Quickstart.Samples.ComplexService.EchoAnimal( object, //params OnComplete, //Complete event OnTimeout //Timeout event ); return false; } function OnComplete(result) {
//你可以直接在js里获得他的属性
alert("Name= " + result.Name + " Color= " + result.Color); } function OnTimeout(result) { alert("Timed out"); } </script> </body> </html>


一帆(老鼠粮仓之路) 2005-10-28 11:33

文章来源:http://pwqzc.cnblogs.com/archive/2005/10/28/263698.html
只有注册用户登录后才能发表评论。