I want to retrieve list data by making http calls through angularjs. Is it possible following is my code in Visual Studio for sharepoint hosted app,
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="../Scripts/angular.js"></script>
<script type="text/javascript" src="../Scripts/angular-route.js"></script>
<meta name="WebPartPageExpansion" content="full" />
<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
<!-- Add your JavaScript to the following file -->
<script type="text/javascript" src="../Scripts/App.js"></script>
<script type="text/javascript">
//*****************************SharePoint App Code***************************************************************************//
var hostweburl;
var appweburl;
// Load the required SharePoint libraries
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl =
decodeURIComponent(
getQueryStringParameter("SPHostUrl")
);
appweburl =
decodeURIComponent(
getQueryStringParameter("SPAppWebUrl")
);
});
var myAngApp = angular.module('SharePointAngApp', []);
myAngApp.controller('spCustomerController', function ($scope, $http) {
$http({
method: 'GET',
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('Customers')/items" +
"?@target='" + hostweburl + "'" +
"&$select=CustomerID,CustomerName,CustomerAddress,CustomerState,CustomerCountry",
headers: { "Accept": "application/json;odata=verbose" }
}).success(function (data, status, headers, config) {
alert("success");
$scope.customers = data.d.results;
}).error(function (data, status, headers, config) {
alert("fail");
});
});
/* myAngApp.config(function ($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}); */
// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
//***************************************************************************************************************************//
</script>
<%-- The markup in the following Content element will be placed in the TitleArea of the page --%> Page Title
<%-- The markup and script in the following Content element will be placed in the of the page --%>
<div>
<p id="message">
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</p>
</div><br />
Title Employee Company
</tr>
<tr ng-repeat="customer in customers">
<td>{{customer.Title}}</td>
<td>{{customer.Employee}}</td>
<td>{{customer.Company}}</td>
</tr>
</table>
</div>