Call a page method from jQuery in Sharepoint

I have an application page (aspx) deployed in the _LAYOUTS folder of MS SharePoint 2010.

I would like to call a method inside this page marked with the [WebMethod] attribute using jQuery. I use the following code on document.ready() :

 $("#btnOk").click(function () { var theUrl = '/_layouts/MyProject/MyPage.aspx/MyMethod'; $.ajax({ type: "get", dataType: "json", url: theUrl, data: {}, success: function (response) { [...] }, error: function (xhr, textStatus, errorThrown) { [...] } }); }); 

This code, unfortunately, does not work. The problem is with the url: it actually works if I use an absolute url like this

 var theUrl = 'http://server/sites/xxx/_layouts/MyProject/MyPage.aspx/MyMethod'; 

How can I convert my path to absolute?

+4
source share
1 answer
 /_layouts/MyProject/MyPage.aspx/MyMethod 

in your example is equivalent to:

 http://server/_layouts/MyProject/MyPage.aspx/MyMethod 

it's your problem. starting with / means the beginning at the root. You must adjust this. If this has to be somehow dynamic, because it can be used in several places, you may need to use codebehind to enter a path or something else. If it always starts from a static location, just change the URL.

If the page from which you use it is, for example:

 http://server/sites/xxx/Somepage.aspx 

Then just change it to

 _layouts/MyProject/MyPage.aspx/MyMethod 

without a slash.

If you are in a subfolder, for example:

 http://server/sites/xxx/Pages/Somepage.aspx 

You can do it:

 ../_layouts/MyProject/MyPage.aspx/MyMethod 

the .. will lead you to one folder.

+6
source

All Articles