我正在开发一个用 VB.Net 编写的 ASP.NET Webforms 项目。我想使用 Ajax 在后台代码中调用 Web 方法。按钮单击事件正确调用代码隐藏方法。然而完成后,
OnSucceeded
和OnFailed
方法都不会被调用。
当我使用 Chrome 工具进行调试并将鼠标悬停在
PageMethods.DisableUserStatus()("", OnSucceeded, OnFailed);
行上时,我看到以下错误:
未捕获类型错误:PageMethods.DisableUserStatus() 不是函数
这是我的代码:
<WebMethod()>
Public Shared Function DisableUserStatus() As JavaScriptSerializer
Users.UpdateUserStatus("EXTERNALUSER", False)
Dim stats As JavaScriptSerializer = New JavaScriptSerializer()
stats.Serialize("Success")
End Function
ASPX 页面标记:
<script type="text/javascript">
$(document).ready(function () {
$('#btnDisableUserStatus').click(function () {
let result = confirm('Are you sure you want to disable users?');
if (result == true) {
DisableUserStatus();
}
});
});
function DisableUserStatus() {
PageMethods.DisableUserStatus()("", OnSucceeded, OnFailed);
}
function OnSucceeded(result) { alert(result) }
function OnFailed(error) { alert(error.get_message()) }
</script>
好吧,首先,您不需要也不想“序列化”返回值,因为该部分是自动为您内置的。
所以,说出这个标记:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="true">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="test web method"
OnClientClick="mytest();return false" />
<input id="Button2" type="button" value="button 2"
onclick="mytest()" />
<script>
function mytest() {
PageMethods.DisableUserStatus(MyGoodResult, MyBadResult)
}
function MyGoodResult(Result) {
alert(Result)
}
function MyBadResult(MyError) {
alert(MyError)
}
</script>
</form>
请注意我们必须如何放入脚本管理器,并且我们必须设置 EnablePageMethods="true"
如前所述,只需将 webmethod 定义为您想要的任何类型,因此在本例中,类型 string 就足够了。
因此,背后的代码是这样的:
<WebMethod()>
Public Shared Function DisableUserStatus() As String
'Users.UpdateUserStatus("EXTERNALUSER", False)
Return "Success from Server"
End Function
现在的结果是这样的:
这里有一个例子展示了如何自动返回值。让我们返回 HotelName、FirstName 和 LastName。
所以,说一下后面的代码:
Structure MyHotel
Dim HotelName As String
Dim FirstName As String
Dim LastName As String
End Structure
<WebMethod()>
Public Shared Function GetHotel(HotelID As Integer) As MyHotel
Dim strSQL As String =
"SELECT HotelName, FirstName, LastName FROM tblHotelsA
WHERE ID = @ID"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = HotelID
Dim rstData As DataTable = MyRstP(cmdSQL)
Dim OneHotel As new MyHotel
If rstData.Rows.Count > 0 Then
OneHotel.HotelName = rstData.Rows(0)("HotelName")
OneHotel.FirstName = rstData.Rows(0)("FirstName")
OneHotel.LastName = rstData.Rows(0)("LastName")
End If
Return OneHotel
End Function
请注意,我们返回的是 MyHotel 类型,因此序列化是自动的。
客户端代码可以是:
<asp:Button ID="cmdGetHotel" runat="server" Text="Get Hotel information"
OnClientClick="gethotel();return false" />
<script>
function mytest() {
PageMethods.DisableUserStatus(MyGoodResult, MyBadResult)
}
function MyGoodResult(Result) {
alert(Result)
}
function MyBadResult(MyError) {
alert(MyError)
}
function gethotel() {
var testid = 20203 // an existing ID in database for testing
PageMethods.GetHotel(testid,MyHotelResult, MyError)
}
function MyHotelResult(MyOneHotel) {
var mystring = "Hotel name = " + MyOneHotel.HotelName + "\n" +
"User name = " + MyOneHotel.FirstName + " " + MyOneHotel.LastName
alert(mystring)
}
function MyError(errInfo) {
alert(errInfo)
}
</script>
注意我们现在如何返回 3 个值。结果是这样的: