[英]How to access respsone from AngularJS POST request?
我有一個用於登錄我的Web應用程序的服務,其中包含以下代碼:
angular.
module('core.user').
factory('User', ['$resource',
function($resource) {
var url = 'http://xxx.xx.xx.xxx:3000/api/authenticate'
return $resource(url, {}, {
save: {
method: 'POST',
params: {},
}
})
}
]);
該請求不是通過本網站上大多數答案所引用的$ http.post()方法來發出的。
我希望訪問此請求的響應,因為它帶有令牌(JWT),並且可以在Postman中看到它的作用。 我可以使用<serviceName>.$promise.then((data) => {// access data here})
使用類似服務的GET請求的響應,但是當我使用POST請求時,不能使用.then()
或.success()
以下是我嘗試訪問POST響應的方式:
angular.
module('signIn').
component('signIn', {
templateUrl: 'sign-in/sign-in.template.html',
controller: ['$resource', 'Company', 'User', '$scope', '$rootScope', '$window',
function CompanyListController($resource, Company, User, $scope, $rootScope, $window) {
// Initialize Variables //
var self = this;
self.companies = Company.query();
self.companies.$promise.then(function(data) {
var hold = data
data = [];
for (var i = 0; i < hold.length; i ++) {
if (hold[i].CompanyID) {
data.push(hold[i])
}
}
self.companies = data;
})
self.user = new User();
// Initialize Variables //
// Sign In Function //
self.signIn = function() {
if (self.user.name && self.user.password && self.companyID){
self.user.$save()
location.href = "#!/" + self.companyID
}
}
// Sign In Function //
}
]
});
如何從POST請求的響應中訪問令牌?
你可以這樣
self.user.$save(function(response){
//here is the response
}, function(error){
})
或像這樣
self.user.$save().$promise.then(function(response){
//here is the response
}, function(error){
})
self.User.save(self.user).then(function(response){})
應該管用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.