开发者

JSON can call method in .aspx file but not in .asmx (ASP.NET web service) file

I am using JQuery & JSON (POST) to call webmethod. However I can call only webmethod located at aspx file but not in asmx file

Below are sample codes

CustomValidate.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;
            }

It always return javascript undefined error. But if I put the AJAX_Test webmethod in aspx page and replace the url: "CustomValidate.asmx/AJAX_Test" to "mypage.aspx/AJAX_Test". It works fine. Any idea?


You are using what is known as a "Page Method". That is, a static (Shared) method with a [WebMethod] attribute on it. These only work inside of an .ASPX page. They are intended to be used only by JavaScript running on the page.

Try removing the Shared from the method and see if it works better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜