Angular2-Http POST请求参数 [英] Angular2 - Http POST request parameters

查看:64
本文介绍了Angular2-Http POST请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出POST请求,但无法正常工作:

I'm trying to make a POST request but i can't get it working:

testRequest() {
      var body = 'username=myusername?password=mypassword';
      var headers = new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');

      this.http
        .post('/api',
          body, {
            headers: headers
          })
          .subscribe(data => {
                alert('ok');
          }, error => {
              console.log(JSON.stringify(error.json()));
          });
}

我基本上想复制此http请求(而不是ajax),就像它是由html表单发起的一样:

I basically want to replicate this http request (not ajax) like it was originated by a html form:

URL:/api

参数:用户名和密码

推荐答案

我认为主体不适合application/x-www-form-urlencoded内容类型.您可以尝试使用此方法:

I think that the body isn't correct for an application/x-www-form-urlencoded content type. You could try to use this:

var body = 'username=myusername&password=mypassword';

希望它对您有帮助, 蒂埃里

Hope it helps you, Thierry

这篇关于Angular2-Http POST请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆