I am encountering an issue in my Angular application where the word 'Undefined' is being appended to a specific WebAPI call, causing both registerUser and login requests to fail. Here are the problematic request URLs:
Request URL: http://localhost:25971/undefined/api/Account/Register
Request URL: http://localhost:25971/undefined/Token
The following code snippet shows the mainCtrl controller:
(function () {
"use strict";
angular
.module("userManagement")
.controller("mainCtrl", ["userAccount", mainCtrl])
function mainCtrl(userAccount) {
var vm = this;
vm.isLoggedIn = false;
vm.message = '';
vm.userData = {
userName: '',
email: '',
password: '',
confirmPassword: ''
};
vm.registerUser = function () {
vm.userData.confirmPassword = vm.userData.password;
userAccount.registration.registerUser(vm.userData, function (data) {
vm.confirmPassword = "";
vm.message = "...Registration successful";
// vm.login();
});
}
vm.login = function () {
vm.userData.grant_type = "password";
vm.userData.userName = vm.userData.email;
userAccount.login.loginUser(vm.userData, function (data) {
vm.isLoggedIn = true;
vm.password = "";
vm.message = "";
vm.token = data.access_token;
});
}
}
}());
Below, you can find the code for the userAccount factory method:
(function () {
"use strict";
angular.module("common.services")
.factory("userAccount", ["$resource", "appsettings", userAccount])
function userAccount($resource, appsettings) {
return {
registration: $resource(appsettings.serverPath + "/api/Account/Register", null,
{
'registerUser': { method: 'POST' }
}),
login: $resource(appsettings.serverPath + "/Token", null,
{
'loginUser': {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function (data, headersGetter) {
var str = [];
for (var d in data) {
str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return str.join("&")
}
}
}
})
}
}
})();
The appsettings constant is defined as follows:
(function () {
"use strict";
angular.module("common.services", ["ngResource"]).constant("appsettings", {
serverpath:"http://localhost:25879/"
});
}());
If anyone familiar with Angular and WebAPI could offer help or guidance on troubleshooting this issue, it would be greatly appreciated!