从JavaScript访问ASMX Web服务

3

我创建了一个名为 WebService 的 Web 服务,其中包含 GetTest 和 SetTest 函数,用于设置和获取 GUID。现在我想在 .aspx 文件的 JavaScript 中使用此函数。 请问如何在 JavaScript 中使用此函数?以下是 Web 服务代码:

[WebMethod]
public void SetTest(Guid id, string text)
{
    this.Application.Add(id.ToString(), text);
}
[WebMethod]
public string GetTest(Guid id)
{
    return this.Application[id.ToString()].ToString();
}

[WebMethod]
public Guid CreateNew()
{
    return Guid.NewGuid();
}
[WebMethod]
public string HelloWorld() {
    return "Hello World";
}

AND .ASPX 代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UsingWebService.aspx.cs" Inherits="UsingWebService" %>

<!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 runat="server">
    <title>Using Web Service</title>    
    <script type="text/javascript" language="javascript">
    debugger;
        var txtGetTestID = '<%= this.txtGetTest.ClientID %>';
        var txtSetTestID = '<%= this.txtSetTest.ClientID %>';
        var _guid = null;

        function GetNew()
        {
           //WebService.CreateNew(GetNewDone,OnError,null);
           GetNewDone(WebService.CreateNew());
        }
        function GetNewDone(result)
        {
            _guid = result;
        }

        function SetTest()
        {
            WebService.SetTest(_guid,$get(txtSetTestID).value);
        }

        function GetTest()
        {
            //WebService.GetTest(_guid,GetTestDone,OnError ,null);
            GetTestDone(WebService.GetTest(_guid));
        }

        function GetTestDone(result)
        {
            $get(txtGetTestID).value = result;

        }
        function OnError(ex)
        {
            alert('Error: '+ex._message);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblInput" Text="Input String" runat="server"></asp:Label>
        &nbsp;
        <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblResult" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="btnInvoke" Text="Invoke" runat="server" 
            onclick="btnInvoke_Click" />
    </div>
    <table>
    <tr>
        <td>
            &nbsp;
        </td>
        <td>
            <asp:Button id="btnNew" runat="server" Text="New" OnClientClick="GetNew(); return false;" />
        </td>
    </tr>
     <tr>
        <td>
            <asp:TextBox ID="txtSetTest" runat="server" />
        </td>
        <td>
            <asp:Button ID="btnSetTest" runat="server" Text="Set" OnClientClick="SetTest(); return false;" />
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox ID="txtGetTest" runat="server" />
        </td>
        <td>
            <asp:Button ID="btnGet" runat="server" Text="Get" OnClientClick="GetTest(); return false;" />
        </td>
    </tr>
</table>
</form>
</body>
</html>

重新标记。C#3.5不存在(请参见https://dev59.com/hnVC5IYBdhLWcg3wliKe)。 - Vaccano
2个回答

5
为了能够从javascript调用WebService,您必须首先添加[ScriptMethod]注释,例如:
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
}

为了调用Web服务,你必须将其包含在ScriptManager中。
   <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/Script/jquery-1.3.2.js" />
            <asp:ScriptReference Path="~/Script/jquery-ui-1.7.2.custom.min.js" />
            <asp:ScriptReference Path="~/Script/json.jquery.js" />
        </Scripts>
        <Services>
            <asp:ServiceReference Path="~/WebService.asmx" />
        </Services>
    </asp:ScriptManager>

现在您需要按照以下方式调用Web服务。
[WebServiceNameSpace].MyWebService.MyWebMethod(
parameters,
function (e)//Function for success
{
},
function (e)//Function for failure
{
});

对于您来说,这将会是:

var id=1;
var text="bla bla";
NameSpace.WebService.SetTest(id, text,
function (e){
},
function (e){
});

您也可以使用jQuery调用Web服务。请查看这里

希望对您有所帮助


0

首先,您需要向 Web 服务方法添加 [ScriptMethod] 属性。然后,在 ASPX 页面上使用 ScriptManager 控件注册您的 Web 服务:

<asp:ScriptManager ID="SM1" runat="server">
  <Services>
    <asp:ServiceReference Path="Service.asmx" />
  </Services>
</asp:ScriptManager>

然后,您可以从JavaScript中调用GetTest()、SetTest()等函数。

希望这有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接