javascript - how to convert the JSON object into form data and send it as Data to web api fetch
问题描述
I am having a json object in the client side and i want to receive that in the Web api controller like controllerContext.HttpContext.Request["Name"] How to send that Json from web API.
The Data expected as "name=abc&type=xyz" for the fetch api. How to convert the JSON object
{
name: abc,
type:xyz
}
below code is not working
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify({
name:abc,
type:xyz
})
};
body: "name=abc&type=xyz"
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
}
How to convert the Json object as above and what is the reason for that .?
解决方案
我不知道 ASP.NET,但我相信有一种方法可以将数据类型配置为 JSON 而不是 form-urlencoded。或者您需要自定义服务器解析数据的方式。
如果无法更改为 application/json。您可以使用querystring包将您的 JSON obj 字符串化为 urlenconded 格式。
推荐阅读
- python - 使用 Python Sound-Device 进行实时音频处理不起作用
- python - 从字符串 Python 中删除非 ascii 和特殊字符
- python-2.7 - 使用 block1 传输在 Copper to coap 服务器中设置的所有选项是什么?
- javascript - Javascript:使用逻辑非的困惑!操作员
- android - rtlsdr 无法在 qt android c++ 中打开
- c++ - 转置矩阵失败
- octave - 如何在 Octave 中正确绘制这个简单的东西?
- histogram - 一些 TensorBoard 直方图是空的 - 如何解释
- python - 在Django中根据类别添加产品
- c - 我可以在 C17 中将“枚举”变量视为“int”吗?