0
我想發送一個帖子請求到另一個服務(一個Spring應用程序),一個身份驗證,但我在建立一個功能Angular2發佈請求時遇到了麻煩。我使用this video作爲參考,這是非常新的,所以我認爲這些信息仍然有效。我也能夠執行沒有問題的獲取請求。難以創建功能Angular2的帖子
這裏是我的POST請求:
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
有誰知道我做錯了嗎?
謝謝,這工作!在哪些情況下我必須使用http.post? – napstablook