我创建了一个名为 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>
<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>
</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>