【发布时间】:2017-11-24 15:42:39
【问题描述】:
要以角度发送 http post 请求的正文,我使用以下实现:
let requestBody: String = "";
//dataObjectis the object that contains the values to send from the form
for (let key in dataObject) {
if (dataObject[key]) {
requestBody += (body.length ? '&' : '') + key + "=" + dataObject[key];
}
}
然后我在我的http post请求中使用requestBody,如下所示:
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let requestOptions = new RequestOptions({ headers: headers });
//http is an Http instance
http.post(URI, requestBody, requestOptions)
我想知道是否有更好的方法或“准备使用”方法允许在 post 方法中直接使用对象 dataObject 而不是使用上述实现。
【问题讨论】:
-
你可以试试 JSON.stringify() ;)
-
我认为 JSON.stringify() 不适用于
application/x-www-form-urlencodedContent-Type(我刚刚编辑了我的问题以添加 Content-Type) -
您可以“发布”许多不同类型的数据。您是否考虑过发布“应用程序/json”内容类型?如果你的 dataObject 已经是 JavaScript Object 类型,则无需将其转换为表单数据。
标签: angular typescript http-post httprequest