Skip to content

Instantly share code, notes, and snippets.

@fogonthedowns
Created October 2, 2014 07:12
Show Gist options
  • Save fogonthedowns/19c9777ab5c9cc6fbf59 to your computer and use it in GitHub Desktop.
Save fogonthedowns/19c9777ab5c9cc6fbf59 to your computer and use it in GitHub Desktop.
API Endpoints
var myApp = angular.module('myApp',[]);
myApp.controller('QuestionsController', ['$scope', '$http', function($scope, $http) {
var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
var user_id = "1"
console.log(AUTH_TOKEN);
$http.defaults.headers.common['X-CSRF-Token'] = AUTH_TOKEN;
$http.defaults.headers.post["Content-Type"] = "application/json";
// Returns Email list Object
// {"config":{}, "data":{"email":"[email protected]"}, "headers":{}, "status":201}
$http({
url: '/lists.json',
method: "POST",
data: {"email":"[email protected]"}
})
.then(function(response) {
console.log(response);
},
function(response) {
console.log(response);
}
);
// Returns Question Object
// {"config":{}, "data":{"body":"new question body","id":"17", "open":true}, "headers":{}, "status":201}
$http({
url: '/questions.json',
method: "POST",
data: {"body":"What is the Molarity of 0.5L H2O mixed with 1L 3M NaOH?","user_id":user_id}
})
.then(function(response) {
console.log(response);
},
function(response) {
console.log(response);
}
);
// GET returns Array of Question Objects:
// [ {"body":"the question body", "id":"1", "open":true, "url":"http://..."},{}]
$http({method: 'GET', url: '/questions.json'}).
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment