Monday, November 10, 2014

Ajax post in sharepoint


$('#sendDataBtn').click(function () {
 selecteddata = document.getElementById("<%=textbox.ClientID%>").value; //might be over 1800 caracters
 $.ajax({
      url: '<%=SPContext.Current.Web.Url%>/_admin/CreateFilefromRecievedData.aspx',
      type: 'POST',
      dataType: 'text',
      data: { "data":selecteddata } ,
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },

      error: function () {
          alert("error");
      },
      success: function (data) {
         alert(data);
      }
 });
Using an AJAX post or Get method is same as we are using it in asp.net application. But there is a slight difference. As i was stuck when used AJAX post method first time in SharePoint 2010.

To use AJAX or JQuery Get or Post method you just write your Get or Post method on a ascx control page as usual nothing special.
But you can not write your web method on a visual webpart or user control or any webpart.
To call your web method from AJAX or Jquery post method you need to write your web method on an ApplicationPage.
For that create an application page in your sharepoint project, write your web method in .cs file of applicationpage.
And call that web method like this from your post or get method:
url: "/_layouts/MyAppPageFolder/AMyappPage.aspx/MyAppMethod"

No comments:

Post a Comment