[英]Difficulty creating a functional Angular2 post
我試圖將發布請求發送到另一個服務(Spring應用程序),進行身份驗證,但是我在構造功能性Angular2發布請求時遇到了麻煩。 我正在使用該視頻作為參考,這是非常新的內容,因此我認為信息仍然有效。 我也可以毫無問題地執行get請求。
這是我的發帖請求:
export class LogIn {
authUser: string;
authPass: string;
token: any;
constructor(private _http:Http){}
onSubmit() {
var header = new Headers()
var json = JSON.stringify({ user: this.authUser, password: this.authPass })
var params2 = 'user=' + this.authUser + '&password=' + this.authPass
var params = "json=" + json
header.append('Content-Type', 'application/x-www-form-urlencoded')
this._http.post("http://validate.jsontest.com", params, {
headers: header
}).map(res => res.json())
.subscribe(
data => this.token = JSON.stringify(data),
err => console.error(err),
() => console.log('done')
);
console.log(this.token);
}
}
該信息是從表單中正確獲取的,我對此進行了幾次測試以確保。 我還使用兩種不同的方式來構建json(params和params2)。 當我嘗試將請求發送到http://validate.jsontest.com
,控制台將打印undefined
this.token
位置。 當我嘗試將請求發送到Spring應用程序時,出現錯誤:
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
有人知道我在做什么錯嗎?
實際上,您需要使用GET方法來執行此操作:
var json = JSON.stringify({
user: this.authUser, password: this.authPass
});
var params = new URLSearchParams();
params.set('json', json);
this._http.get("http://validate.jsontest.com", {
search: params
}).map(res => res.json());
請參閱以下代碼: http ://plnkr.co/edit/fAHPp49vFZJ8OuPC1043?p=preview。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.