Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
 $.ajax({
      type:"POST",
      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.

share|improve this question
add comment

closed as off topic by RJ Cuthbertson, PirateEric, John Chapman, Muhammad Raja, ali Sharepoint Mar 9 '13 at 16:22

Questions on SharePoint Stack Exchange are expected to relate to SharePoint within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 2 down vote accepted

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;

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

In the aspx.cs:

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

References:

share|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
add comment

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".

share|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
add comment

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