views:

84

answers:

1

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?

A: 

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.

John Saunders
Good explanations. I'm clear now. Thanks
Alfred