. My question is, where is this documented? Maybe I'm just missing something here, but I spent a lot of time on this for a solution and this was the only one I could get to work. I never could find this in the Angular docs, but I have missed some things before. The solution came due to help from another dev who couldn't remember how they found this pattern.
//custom directive
$scope.login = function () {
var loginResponse = {};
var errorMessage = "";
var access_token = "";
loginService.login(myInfo).then(function(response) {
console.log("response is: " + JSON.stringify(response));
if(response.data.token) {
$window.sessionStorage.setItem("token", response.data.token);
}else{
errorMessage = response.data.errorMessage.split(":").pop();
console.log("errorMessage is: " + errorMessage);
... more code
//service
function loginService($window, $http, $httpParamSerializerJQLike) {
var service = {
login:login
};
return service;
function login(myInfo) {
... more code here
params = $httpParamSerializerJQLike(params);
console.log("The params are: " + params);
return $http({
url: baseURL,
method: 'POST',
data: params,
//Set the headers so Angular is passing information as form data, not request payload.
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function onSuccess(response) {
return response;
}).catch(function onError(response) {
return response;
});
}