How to call WebApi in SharePoint 2013 using Javascript

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 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,

Comments