-1

I am trying to call a webmethod written on application page but getting an error,

Status Code:500 Internal Server Error

JavaScript:

<script type="text/javascript">
$(document).ready(function () {

    $.ajax({
         type: "POST",
         url: "SiteUrl/_layouts/15/SharepointProject1/WebMethods.aspx/Hello",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         async: true,
         success: function (msg) {
    alert(msg.d);
             response(msg.d);

         },
         error: function (xhr, status, error) {
              alert(xhr.status);
        alert(xhr.responseText);
         }
     });
    });
</script>

C# Code:

 public partial class WebMethods : UnsecuredLayoutsPageBase
    {

        protected override bool AllowAnonymousAccess { get { return true; } }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string Hello()
        {
            return "Hello External";
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       public static string GetDate()
       {
           return DateTime.Now.ToString();
       }
    }
0

Couple of issues

  1. Person class should be set as Serializable
  2. GetDate method accepts 0 parameters but you are passing two values from the Ajax call.

Sample Service

public partial class Services : LayoutsPageBase
{
    [WebMethod]
    public static string Hello()
    {
        return "Hello";
    }
}

Sample Ajax Call

var url = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/projectfolder/services.aspx/Hello";
$.ajax({
    url: url,
    contentType: "application/json; charset=utf-8",     
    dataType: "json",
    method: "POST",
    headers: {
        "Accept": "application/json; odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        alert("success");
    },
    error: function (data, arg) {
        alert("error");
    }
});
  • I have modified the code but getting the same error,can you please check now? – vishal goyal Jun 17 '16 at 9:51
  • Check my sample – Amal Hashim Jun 17 '16 at 12:14
  • Hi,Getting below error {Message: "There was an error processing the request.", StackTrace: "", ExceptionType: ""} ExceptionType : "" Message : "There was an error processing the request." StackTrace : "" – vishal goyal Jun 18 '16 at 10:10
  • Today I have tried with my old code and found it's working fine,now I am trying to find what's the reason of this different behaviour? – vishal goyal Jun 20 '16 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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