Skip to content

Instantly share code, notes, and snippets.

@kkurni
Created January 30, 2013 07:37
Show Gist options
  • Save kkurni/4671471 to your computer and use it in GitHub Desktop.
Save kkurni/4671471 to your computer and use it in GitHub Desktop.
Inject HTML Service Example with Authorization
'use strict';
SEEK.factory('InjectHtmlService', ['mobileRoot', '$http', 'TokenHandler', function (mobileRoot, $http, tokenHandler) {
var resource = {};
resource.injectHtml = function (url, success, error) {
$(document).on('submit', 'form', function () {
//add site
submitPost(mobileRoot + $(this).attr('action'));
return false;
});
var token = 'SWS ' + tokenHandler.getToken();
$http.get(url, { withCredentials: true, headers: {Authorization: token } }).
success(function (data, status) {
resource.htmlData = data;
success(data, status);
}).
error(function (data, status) {
resource.htmlData = data || "Request failed";
error(data, status);
}
);
return resource;
};
var submitPost = function (url) {
var $form = $('form');
// serialize the data in the form
var serializedData = $form.serialize();
// Setup CSRF safety for AJAX:
var token = 'SWS ' + tokenHandler.getToken();
$http.post(url, serializedData, { withCredentials: true, headers: {Authorization: token, 'Content-Type': 'application/x-www-form-urlencoded;' } }).
success(function (data, status) {
resource.htmlData = data;
}).
error(function (data, status) {
resource.htmlData = data || "Request failed";
}
);
};
return resource;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment