-1

我已经了解了通过 req.body 发送表单值。我怎样才能向它附加一个新的值/json?例如。如果表单只包含一个或两个输入字段并提交,它发送一个发布请求,我如何将一个 json 对象附加到快递服务器收到的 req.body 中?

我正在设计一个带有 express 的 rest api,以便 angular 访问。我需要发送 json 来表达发布请求。这是一个购物车(作为产品和费率的 json 对象),我必须将其发送到后端才能下订单(存储在 mongo 中)。

有什么建议么?

4

1 回答 1

0

您必须将内容类型设置为 application/json。您可以从表单数据构建您的 JSON 对象:

var data = {
  id: form-model-id,
  name: form-model-name
}

有关更多信息,请参阅ng-http

$http.post('http://127.0.0.1:3000/something/cool', data, {
  headers: {'Content-Type': 'application/json'},
}).then(function (response) {
  callback(null, response.data);
}, function (response) {
  if (response.status === 0) {
    throw { message: 'api_timeout' };
  } else {
    if (response.status === 409) {
      throw { message: 'api_login_failed' };
    } else {
      callback(response.status, response.data);
    }
  }
});
于 2013-08-17T17:56:00.513 回答