How to call WebApi in SharePoint 2013 using Javascript


Sathish Nadarajan
SharePoint MVP
Published On :   02 Jul 2015
Visit Count
Today :  2    Total :   12115
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In the previous article, we saw how to create a WebAPI. Now, let us see a small piece of code, how to call that API from your SharePoint 2013 site using Javascript.

I assume that, we have created the API successfully and the API is hosted properly. Now, on our Javascript, a simple call like below will do the rest of the things.

 try{
 			 $.getJSON("http://localhost:47270/api/test/MyAction/Test?callback=?",
              function (Data) {
               alert(Data);
              })
              .fail(
              function (jqXHR, textStatus, err) {
              alert('Fail ' + err);
              });
 			 }catch(err)
 			 {
 			 
 			 }
 

The attribute “?callback?” is added at the last, to make the JSONP return. If we expect only the JSON object, then there is no need of the “?callback?” attribute.

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Protect Your SharePoint