1

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?

3
  • 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 Oct 10, 2017 at 8:19
  • Thanks it worked for me. why don't you post it as an Answer.
    – Madhav
    Oct 10, 2017 at 8:59
  • Posted it as an answer Oct 10, 2017 at 9:23

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.