【问题标题】:"Unsupported Media Type", 415 eror when post request in react-native“不支持的媒体类型”,在 react-native 中发布请求时出现 415 错误
【发布时间】:2021-10-05 09:38:21
【问题描述】:

我正在尝试在 react-native 应用程序中获取我的 asp .net 核心 api。当我用邮递员进行测试时,这个 api 工作正常。但是在 react-native 中我有这个错误。

["https://tools.ietf.org/html/rfc7231#section-6.5.13", "Unsupported Media Type", 415, "00-2608a636e0aeaf4bb2b29a2e1b54370a-2145e9063626d940-00"]

这是我的 API。我已经尝试过 public JsonResult 但没有任何改变。

 [HttpPost("register")]
        public IActionResult Register([FromBody] TbPersonel model)
        {
            try
            {
                // SOME CASES

                if (dt.Rows.Count > 0)
                {
                    model.login = true;                   
                    return new JsonResult(model.login);
                }
                else
                {
                    model.login = false;                   
                    return new JsonResult(model.login);
                }
            }
            catch (Exception e)
            {
                Logla.Log(MethodBase.GetCurrentMethod().Name, ControllerContext.ActionDescriptor.ControllerName, e);
                return new JsonResult(e);
            }
        }

反应原生

 fetch('http:/.../api/login/register/',{
            method:'post',
            header:{
                'Accept': 'application/json',
                // 'Content-type': 'application/json'
        'Content-Type': 'application/json; charset=utf-8'
            },
            body:JSON.stringify({               
                'kullaniciAdi': userName,
                'sifre': userPassword
            })          
        })    
        .then((response) => response.json())
         .then((responseJson)=>{
       console.log(Object.values(responseJson));

Postman Post request

Postman headers

【问题讨论】:

  • 只给'Content-Type': 'application/json;。并尝试{ "kullaniciAdi": userName, "sifre": userPassword }
  • 同样的结果,不工作。
  • 您可以在浏览器控制台查看网络标签
  • @İbrahimEthemBildirici 你能提供 TbPersonel 的尸体吗
  • 公共部分类 TbPersonel { public string KullaniciAdi { get;放; } 公共字符串 Sifre { 获取;放; } 公共字符串 Aciklama { 获取;放; } 公共布尔? Aktif { 得到;放; } 公共布尔?斯林迪{得到;放; } 公共布尔登录 { 获取;放; }..... 更多行 }

标签: c# json react-native api fetch


【解决方案1】:

问题出在 fetch 的参数上。 fetch 没有任何名为 header 的参数,但它是带有 s 的 headers。因此,您的代码没有要求正确的 Content-type 和 Accept Headers。要解决这个问题,只需在标题键中添加一个 s :

fetch('http:/.../api/login/register/',{
        method:'post',
        headers:{
            'Accept': 'application/json',
            // 'Content-type': 'application/json'
    'Content-Type': 'application/json; charset=utf-8'
        },
        body:JSON.stringify({               
            'kullaniciAdi': userName,
            'sifre': userPassword
        })          
    })    
    .then((response) => response.json())
     .then((responseJson)=>{
   console.log(Object.values(responseJson));

很遗憾,Javascript 没有突出显示此错误。

【讨论】:

  • 大声笑。非常感谢。
  • 欢迎,我们为此奋斗了哈哈!
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 2023-03-13
  • 2021-08-23
  • 2018-10-13
  • 2018-07-25
  • 2016-09-09
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多