我正在使用 aurelia-fetch-client 将一些数据发送到 web-api (在注册方法中)。
headers: Headers;
register() {
this.headers = new Headers();
this.headers.append("content-type", "application/json; charset=utf-8");
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: this.headers
})
}
如您所见,我想更新请求的标头(在该追加方法调用中),为此,我需要创建自己的 Headers 对象,在其上调用方法追加,然后将其分配给的 headers 属性我的请求。我想直接在请求正文中执行此操作:而不是编写
headers: this.headers
我想写一些类似的东西:
headers: {
append("content-type", "application/json; charset=utf-8");
}
或类似的东西:
headers: new Headers().append(..)
这个想法是避免声明一个新对象来存储我的标头。
我怎样才能做到这一点?
谨致谢意。
您可以直接将带有键和值的 JS 对象文字传递给headers
财产:
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: {
"content-type", "application/json; charset=utf-8"
}
});
或者你也可以装箱Headers
预填充了您的自定义标头的对象:
this.httpClient.fetch("api/Account/Register", {
method: "POST",
body: JSON.stringify({
email: this.email,
password: this.password
}),
headers: new Headers({
"content-type", "application/json; charset=utf-8"
})
});
另请参阅插件的 headers 相关测试 https://github.com/aurelia/fetch-client/commit/b70ac032dd2e0af74ecaa1eea8965bc6e9677a7f#L180-L237.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)