【发布时间】:2016-03-27 13:22:43
【问题描述】:
我有注册表,我需要发送带有 csrf 令牌的表格,我应该在发送填写的表格之前得到它。没有工作代码,但它知道我想要什么。
var model = {
username : this.form.value.username,
email : this.form.value.email,
password_second : this.form.value.password_second,
password : this.form.value.password,
csrf : ''
};
this._csrfService.getToken().subscribe(
csrf => model.csrf,
error => console.log(error)
);
this._signUpService.sendForm(model)
.subscribe(
hero => console.log(hero),
error => console.log(error));
SignUp 和 Csrf 服务很明显:
getToken()
{
console.log(this.http.get(this._sighUpUrl));
return this.http.get(this._sighUpUrl)
.map(res => res.json().data.csrf)
.catch(this.handleError)
}
sendForm(name:Object)
{
let body = JSON.stringify(name);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
console.log(this.http.post(this._sighUpUrl, body, options));
return this.http.post(this._sighUpUrl, body, options)
.map(res => res.json().data)
.catch(this.handleError)
}
当我没有得到 csrf 令牌时如何等待?
【问题讨论】:
-
只是从第一个回调中发送第二个请求
-
我无法从回调中访问“this”
-
你为什么不能访问这个?
-
我有错误:“例外:类型错误:无法读取未定义的属性‘值’”。 console.log(this) 也显示它是 Observable 类,而不是我的组件
-
lambda 中的
this始终指代外部作用域的this。如果this是一个 observable,这意味着你没有使用 lambda,你使用了一个匿名函数。
标签: angular rxjs angular2-services