[英]Difficulty creating a functional Angular2 post
I'm trying to send a post request to another service (a Spring application), an authentication, but I'm having trouble constructing a functional Angular2 post request at all. 我试图将发布请求发送到另一个服务(Spring应用程序),进行身份验证,但是我在构造功能性Angular2发布请求时遇到了麻烦。 I'm using this video for reference, which is pretty new, so I assume the information still valid. 我正在使用该视频作为参考,这是非常新的内容,因此我认为信息仍然有效。 I'm also able to execute a get request with no problems. 我也可以毫无问题地执行get请求。
Here's my post request: 这是我的发帖请求:
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);
}
}
The info is being correctly taken from a form, I tested it a couple of times to make sure. 该信息是从表单中正确获取的,我对此进行了几次测试以确保。 I am also using two different ways to build the json (params and params2). 我还使用两种不同的方式来构建json(params和params2)。 When I try to send the request to http://validate.jsontest.com
, the console prints undefined
where this.token
should be. 当我尝试将请求发送到http://validate.jsontest.com
,控制台将打印undefined
this.token
位置。 When I try to send the request to the Spring application, I get an error on that side: 当我尝试将请求发送到Spring应用程序时,出现错误:
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?
In fact you need to use the GET method to do that: 实际上,您需要使用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());
See this plunkr: http://plnkr.co/edit/fAHPp49vFZJ8OuPC1043?p=preview . 请参阅以下代码: http ://plnkr.co/edit/fAHPp49vFZJ8OuPC1043?p=preview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.