I have been searching through old threads but have not found any with them same issue. My $.ajax is working it just does not find/return the correct function. I am using..

 var loc=window.location.href;
      url: loc+"/myFunc",
      success: function(msg){ alert(msg);}

In the .aspx.cs

 public string myFunct() { return "hello"; }

In the .cs

 public string myFunct() { return "world"; }

I have aslo tried a few other variations of the url but the success alert always shows the entire pages HTML. What am I doing wrong?

Update: My Solution.

Thanks to the responses below I managed to find this link http://www.codeproject.com/Articles/27172/The-ICallbackEventHandler Which is an interface that runs ajax in the background. Super easy to set up.


I think that calling ASP.NET page method from a jQuery requires that the method being called be marked static and with the [WebMethod] attribute.

Also, please note that jQuery can call ASP.NET page methods. Methods in a SharePoint Visual WebPart are technically user controls. Here is another question that covers this topic.

As noted by Tim Ebenezer in this thread, the only way to expose a page method to your Visual WebPart is to create an application page and link directly to that. Personally, I create a blank application page which blocks all GET requests (as all calls to page methods require POST) and store all of my Ajax queries there. An alternative (as mentioned in the link in your comment) is to create an ASMX WebService. I've linked blog posts to both of these methods in references below.

Finally, I think you're also missing some attributes on your ajax call -- it also requires contentType and dataType and an empty data property.

In the jQuery code:

var loc=window.location.href;

    type: "POST",
    url: loc+"/myFunc",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg){ alert(msg.d);}

In the aspx.cs:

public static string myFunct() { return "hello"; }


| improve this answer | |
  • So far as I have looked into it [WebMethod] is no longer supported by share point. I even tried adding classes & references but they all fail. closest I think I have come is dbremes.wordpress.com/2011/01/03/… – user13186 Feb 22 '13 at 3:06

This is only possible using Application Pages deployed in the LAYOUTS folder in SharePoint. Is this how you are deploying your page?

It could be as simple as the typo you have included, the method you are calling is called "myFunct", the URL you are calling is "myFunc".

| improve this answer | |
  • I am using a visual web part do one of the default files included count as an application page? – user13186 Feb 22 '13 at 14:31

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