【发布时间】:2017-01-20 06:13:40
【问题描述】:
我有以下函数,它返回验证的承诺
authenticate: function(options) {
return new Promise((resolve, reject) => {
$.ajax({
url: this.tokenEndpoint,
type: 'POST',
headers: {'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate'},
data: JSON.stringify({
username: options.identification,
password: options.password
})
}).then(function(response) {
console.log(response) // I get here object with good properties
resolve({
lastLoginDate: response.lastLoginDate,
login: response.login,
name: response.name,
role: response.role,
token: response.id_token
});
}, function(xhr, status, error) {
if(error !== undefined) {
console.log(error);
}
var response = xhr.responseText;
reject(response);
});
});
当我调用它并传递正确的用户名和密码时,它返回“未定义”,但是当我传递错误的属性时,我的“拒绝”工作得很好。有人知道为什么我的“then”没有返回预期的输出吗?
this.get('session').authenticate('authenticator:custom', {'identification': identification, 'password': password})
.then((data)=>{
console.log(data); //Undefined <- here is my problem.
})
.catch((reason) => {
console.log(reason); // It works, when pass bad password!
this.set('errorMessage', reason.error);
});
【问题讨论】:
-
authenticate方法只接受一个参数options但你发送两个参数this.get('session').authenticate('authenticator:custom', {'identification': identification, 'password': password})
标签: javascript jquery ember.js promise