Skip to content

Instantly share code, notes, and snippets.

@boycaught
Created May 20, 2013 01:41
Show Gist options
  • Save boycaught/5609923 to your computer and use it in GitHub Desktop.
Save boycaught/5609923 to your computer and use it in GitHub Desktop.
AngularJS HTTP call skeleton, for posting to CouchDB (personal tutorial)
function CreateStoryController($scope, $http) {
// Create callback for success, injected into controller scope.
$scope.submitStory = function() {
var success = function(data, status, headers, config) {
$scope.message = 'Success!';
};
// Create callback for failure.
var failure = function(data, status, headers, config) {
$scope.message = 'Problem';
};
// Define callback to execute the createStory() action over http.
var submitStory = function (storydata, success, failure) {
var payload = $.param(storydata);
var request = {
method: 'POST',
url: '/couchdb/database/',
data: payload,
headers: {'Content-Type': 'application/json'}
};
$http(request)
.success(success)
.error(failure);
}
// Inject the payload into the scope.
// Execute the submitStory() function.
submitStory($scope.storydata, success, failure);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment