我正在使用 JQuery 和 JSON (POST) 来调用 webmethod。但是我只能调用位于 aspx 文件中的 webmethod,但不能调用位于 asmx 文件中的 webmethod
下面是示例代码
自定义验证.asmx
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Public Class CustomValidate
Inherits System.Web.Services.WebService
'ACCESS VIA JSON
<System.Web.Services.WebMethod()> _
Public Shared Function AJAX_Test(ByVal date1) As Boolean
...
Return True
End Function
End Class
Javascript:JQuery JSON
function isDates(source, arguments) {
var isValidDate;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CustomValidate.asmx/AJAX_Test",
data: '{date1: "' + arguments.Value + '"}',
dataType: "json",
async: false,
success: function(result) {
isValidDate = result;
},
error: function(httpRequest, textStatus, errorThrown) {
alert("status=" + textStatus + ",error=" + errorThrown);
}
});
arguments.IsValid = isValidDate;
}
它总是返回 javascript 未定义错误。但是,如果我将 AJAX_Test webmethod 放在 aspx 页面中并将 url:“CustomValidate.asmx/AJAX_Test”替换为“mypage.aspx/AJAX_Test”。效果很好。有什么想法吗?
最佳答案
您正在使用所谓的“页面方法”。即,带有 [WebMethod] 属性的静态(共享)方法。这些仅在 .ASPX 页面内部起作用。它们仅供页面上运行的 JavaScript 使用。
尝试从方法中删除 Shared
并查看是否效果更好。
关于asp.net - JSON 可以调用 .aspx 文件中的方法,但不能调用 .asmx(ASP.NET Web 服务)文件中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172155/