使用aurelia

编程入门 行业动态 更新时间:2024-10-23 17:33:42
本文介绍了使用aurelia-fetch-client发布"x-www-form-urlencoded"内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题很简单:如何使用Aurelia Fetch客户端发布x-www-form-urlencoded内容?

The question is simple: how do I post x-www-form-urlencoded content with Aurelia Fetch client?

我需要将帖子发布到使用OWIN和Katana进行身份验证的简单ASP.NET Web API服务器上.

I need to make the post to a simple ASP.NET Web API server that is using OWIN and Katana for authentication.

我已经尝试过的例子:

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 });

很显然,这没有按预期进行.如何发布示例中显示的数据的正确方法?

Obviously, that didn't work as intended. How is the correct way to go about posting the data presented in the example?

推荐答案

aurelia-fetch-client是基于Fetch规范构建的,似乎Fetch始终将FormData作为Content-Type: multipart/form-data发送.

The aurelia-fetch-client is built on Fetch specification, and it seems that Fetch always sends FormData as Content-Type: multipart/form-data.

要解决此问题,必须将参数转换为查询字符串,然后将内容类型设置为x-www-form-urlenconed.您可以使用jQuery或自定义函数将对象转换为查询字符串.像这样:

To get around this, you have to convert the parameters to a query string and then set the content-type to x-www-form-urlenconed. You can use jQuery or a custom function to convert the object to a query string. Like this:

//jQuery.param returns something like ?param=1&param2=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 });

我知道这不是一个好的解决方案,但这是我到目前为止发现的最简单的方法.

Not a good solution, I know, but that's the easiest way I found so far.

更多推荐

使用aurelia

本文发布于:2023-10-09 18:21:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:aurelia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!