如何在角度2的http发布请求中发送表单数据? [英] How to send form data in a http post request of angular 2?

查看:71
本文介绍了如何在角度2的http发布请求中发送表单数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将更新的用户详细信息的表单数据发送到角度为2的节点服务器的后端,但是我无法发送表单数据并且服务器以500的状态进行响应,在angularjs中,我做了一些事情像这样,服务文件

I am trying to send form data of the updated user details to the back end which node server in angular 2,However I couldn't send the form data and the server responds with status of 500,In angularjs I have done something like this, service file

  update: {
    method: 'POST',
    params: {
      dest1: 'update'
    },
    transformRequest: angular.identity,
    'headers': {
      'Content-Type': undefined
    }
  }

在控制器中

var fd = new FormData();
var user = {
  _id: StorageFactory.getUserDetail()._id,
  loc: locDetails
};
fd.append('user', angular.toJson(user));
UserService.update(fd).
$promise.then(
  function(value) {
    console.info(value);
    updateUserDetailsInStorage();
  },
  function(err) {
    console.error(err);
  }
);

我无法确定如何在角度2中执行此操作,因为angular.toJson,angular.identity和transformrequest功能在角度2中不可用到目前为止,我已经在角度2中完成了以下操作,

I couldn't to figure how to do this in angular 2 as angular.toJson,angular.identity and transformrequest features are not available in angular 2, so far I have done the following in angular 2,

 let fd = new FormData();
 let user = {
   _id: this.appManager.getUserDetail()._id,
   loc: locDetails
 };
 fd.append('user', JSON.stringify(user));
 this.userService.update(fd).subscribe((value) => {
   console.log(value);
   this.updateUserDetailsInStorage();
 }, (err) => {
   console.error(err);
 });

http服务文件

update(body) {
  console.log('update', body);
  const headers = new Headers({
    'Content-Type': undefined
  });

  const options = new RequestOptions({
    headers: headers
  });
  return this.http.post(`${app.DOMAIN}` + 'user/update', body, options)
    .map((res: Response) => {
      res.json();
    }).do(data => {
      console.log('response', data);
    })
}

我阅读了很多文章,尝试了一些尝试,但到目前为止还没有成功,有人可以建议我该怎么做吗?

I have read many posts and tried few things but so far it was unsuccessful, could anyone suggest me how to do this?

推荐答案

如果服务器控制器需要,则可以添加标头,否则您可以像这样简单地将其发布

You can add headers if your server controller requires it else you can simply post it like this

let body = new FormData();
body.append('email', 'emailId');
body.append('password', 'xyz');
this.http.post(url, body);

这篇关于如何在角度2的http发布请求中发送表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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