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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。