[英]Cannot get post working in Angular2
因此,我正在嘗試在我的角度應用程序中進行身份驗證。 我參加了標題,並創建了一個字符串化的正文。 我還查看了其他關於在postman中發送帖子請求的論壇帖子,但我無法使其正常運行。
getToken()
{
console.log('started getting of token');
//get username and password from local storage
//Send username and password via body
let headers = new Headers();
let body = JSON.stringify({
name: 'test',
password: 'test'
});
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.jwt = this.http.post('http://localhost:8080/api/authenticate/', {
headers: headers,
body: body
}).map(res => res.text()).subscribe(data => console.log(data));
}
所以上面是我的代碼。 我知道這可能有點愚蠢,但我無法使其正常運行。 我收到找不到用戶的錯誤。 當用戶名錯誤,並且用戶名不存在於數據庫中時,就會發生這種情況。 這樣也有些棘手。
誰知道我犯了什么愚蠢的錯誤?
使用以下代碼發送帖子請求
this.jwt = this.http.post('http://localhost:8080/api/authenticate/',body,headers).map(res => res.text()).subscribe(data => console.log(data));
將此代碼添加到您的service.ts文件中。
import { Http, URLSearchParams} from '@angular/http';
getToken()
{
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let body = new URLSearchParams();
body.set('name', test);
body.set('password', test);
return this.http.post("http://localhost:8080/api/authenticate/", body, headers).map(response => response.json());
}
這段代碼是我用於發布請求的代碼
send_request_post(url: String, data:{})
{
var headerPost = new Headers
({
'Content-Type' : 'application/json ;charset=utf-8'
})
return this.http.post(url, JSON.stringify(data), {headers: headerPost})
.toPromise()
.then
(
response =>
{
console.log(response);
}
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.