asp.net ajax调用webservice

WebService代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace AJAXEnabledWebApplication1
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld(string str)
        {
            return "Hello " + str;
        }

    }
}

页面代码:

<%@ Page Language="C#" MasterPageFile="~/Manager/MasterPage/PageTemplate.Master" AutoEventWireup="true" CodeBehind="Test222.aspx.cs" Inherits="AJAXEnabledWebApplication1.Test222" Title="无标题页" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageBody" runat="server">
    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
        <Services>
            <asp:ServiceReference Path="WebService.asmx"/>
        </Services>
    </asp:ScriptManagerProxy>
    <script type="text/javascript">
    function test()
    {
        AJAXEnabledWebApplication1.WebService.HelloWorld("胡晓伟",succeeded,failed);
    }
    
    function succeeded(result)
    {
        alert(result);
    }
    
    function failed(error)
    {
        alert("系统出现故障!请手动保存好页面!");
    }
    </script>
    
        <div>
        <input id="Button1" type="button" value="button" onclick="test();" />
    </div>
</asp:Content>

 

posted @ 2009-05-07 17:55  CoderWayne  阅读(318)  评论(0编辑  收藏  举报