Skip to content

Instantly share code, notes, and snippets.

@chrisjlee
Forked from rrcook/AngularTestRestApi.page
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjlee/e38da4e6d4f0f2a11bef to your computer and use it in GitHub Desktop.
Save chrisjlee/e38da4e6d4f0f2a11bef to your computer and use it in GitHub Desktop.
<apex:page applyBodyTag="false" docType="html-5.0" applyHtmlTag="false" standardStylesheets="false" sidebar="false" showheader="false">
<html ng-app="restApiApp" >
<head>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"/>
</head>
<body ng-controller="MainCtrl">
<h1>Test REST API from JavaScript with AngularJS</h1>
<div ng-init="sessionId='{!$Api.Session_ID}'; url='/services/data/v30.0/query?q=SELECT+Name+FROM+Account+LIMIT+10'">
<input id="query" size="120" value="{{url}}" ng-model="url"/>
<button ng-click="submit()">Submit</button>
<p>Results:</p>
<pre>{{results | json}}</pre>
</div>
</body>
<script>
angular.module('restApiApp', []).controller('MainCtrl', function($scope, $http) {
$scope.results = ""; // sessionId, url from ng-init
$scope.submit = function() {
$http.defaults.headers.common.Authorization = 'Bearer ' + $scope.sessionId;
$http.get($scope.url).then(function(response) {
$scope.results = response.data;
}, function(errResponse) {
console.log('Error while fetching data');
});
};
});
</script>
</html>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment