Question

He I am trying to call one of my method in web part using ajax call but there I am getting error as

Test2.aspx/ReadData
 Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I have declared method as like

 [WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string ReadData(string year)
        {
            return year;
        }
    }

and using Ajax call as like

$(document).ready(function () {
    $("div.LoadMore").click(function () {
        alert("callAjaxMethod");
       // var siteCollectionURL = _spPageContextInfo.siteAbsoluteUrl;
        var loc = window.location.href;
        alert(loc);
        $.ajax({
            type: "POST",
            url: "Test2.aspx/ReadData",

            data: {year: '2017' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {

                if (response.d) {
                    alert("Success");

                }
                else {
                    alert("Failed");
                }
            },
            failure: function (response) {
                alert("Failed");

            }
        });
        alert("After callAjaxMethod");
    });
});

Test2.aspx is my page name where i am adding that web part. here i have tried url as

url: "Visualwebpart1.ascx.cs/ReadData",

but there also i am getting the same error. Is there any way to get this work?

Was it helpful?

Solution

Instead of defining your WebMethod in WebPart, create an application page and define the WebMethod in that application page. Use the application page URL like /_layouts/YourSolution/ApplicationPage.aspx/ReadData

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top