Search code examples
c#jquery.netpostmanwebmethod

.NET Web Method not being called from javascript only Page


I have a web method in a codebhind of a .aspx page, but when I try to access it doesn't trigger the webmethod only the page. The return status is 200, the page is being called, but the method is being ignored. It doesn't matter if I use name.aspx/GetData or name.aspx/Anything, the result is 200 but the method not fired. I tested both with jquery Ajax and Postman. Get and Post attempts. Is there anything to alter in the web.config or any other thing.

$.ajax({
                        url: '/adm/clientAccess.aspx/MyMethodInexistent',
                        data: {},
                        type: 'POST',
                        contentType: 'application/x-www-form-urlencoded',
                        dataType: 'html',
                        success: function (data) {
                           //I GET HERE even if the method doesn't exist, and if it exists, it doesn't return data.
                           alert(1);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    }
                    );

enter image description here

enter image description here

enter image description here


Solution

  • Can you try with the below code , make sure the path to the method is correct in the url below. also enable page methods at the scriptmanager level if you are using one.

       $.ajax({
        url: '/adm/clientAccess.aspx/MyMethod',
        data: {},
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: "true",
        success: function (data) {
           console.log(data);
           //alert(1);
        },
        error: function (response) {
            alert(response.responseText);
        }
    }
    );