无法使 post 在 Angular2 中工作
Cannot get post working in Angular2
所以我正在尝试在我的 angular 应用程序中进行身份验证。我参加了 headers 并创建了一个字符串化的 body。我还查看了一些其他论坛 post 关于在 postman 中发送 post 请求,但我无法让它工作。
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));
}
以上是我的代码。我知道这可能有点傻,但我无法让它工作。
我收到无法找到用户的错误。当用户名错误并且数据库中不存在用户名时,就会发生这种情况。所以这也有点棘手。
谁知道我犯了什么愚蠢的错误?
使用以下代码发送post请求
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());
}
此代码是我用于 post 请求
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);
}
)
}
所以我正在尝试在我的 angular 应用程序中进行身份验证。我参加了 headers 并创建了一个字符串化的 body。我还查看了一些其他论坛 post 关于在 postman 中发送 post 请求,但我无法让它工作。
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));
}
以上是我的代码。我知道这可能有点傻,但我无法让它工作。 我收到无法找到用户的错误。当用户名错误并且数据库中不存在用户名时,就会发生这种情况。所以这也有点棘手。
谁知道我犯了什么愚蠢的错误?
使用以下代码发送post请求
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());
}
此代码是我用于 post 请求
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);
}
)
}