问题很简单:如何发帖x-www-form-urlencoded
对 Aurelia Fetch 客户端满意吗?
我需要将帖子发送到一个使用 OWIN 和 Katana 进行身份验证的简单 ASP.NET Web API 服务器。
我已经尝试过的一个例子:
var loginDTO = new FormData();
loginDTO.append('grant_type', 'password');
loginDTO.append('email', 'test');
loginDTO.append('password', 'test');
return this.http
.fetch(config.router.token, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: loginDTO
});
显然,这并没有达到预期效果。发布示例中提供的数据的正确方法是什么?
aurelia-fetch-client 是基于 Fetch 规范构建的,看起来 Fetch 总是发送FormData
as Content-Type: multipart/form-data
.
为了解决这个问题,您必须将参数转换为查询字符串,然后将内容类型设置为x-www-form-urlenconed
。您可以使用 jQuery 或自定义函数将对象转换为查询字符串。像这样:
//jQuery.param returns something like ?param=1¶m2=2 and so on
//params = a plain javascript object that contains the parameters to be sent
this.http.fetch(url, {
body: $.param(params),
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(response => {
//your magic here
});
我知道这不是一个好的解决方案,但这是我迄今为止发现的最简单的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)