React Native发起POST请求调用ASP.NET Core接口返回415 Unsupported Media Type错误
问题原因
415 Unsupported Media Type错误的核心是后端接口要求的请求内容类型和实际发送的请求内容类型不匹配,你遇到的问题根因出在React Native的fetch请求配置写错了:
- fetch的标准请求头配置字段是
headers,你的代码里写成了单数的header,导致你设置的Content-Type: application/json; charset=utf-8完全没有生效,请求默认发送了后端不接受的内容类型,所以触发415报错。
解决方案
直接修改React Native端的请求代码,把header改为headers即可,修正后的代码如下:
fetch('http:/.../api/login/register/',{ method:'post', headers:{ // 此处将原错误的单数header改为复数headers 'Accept': '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)); })
如果修改后仍存在问题,可以额外排查两个点:
- 确认发送的JSON参数的键名,和后端
TbPersonel类的属性名、大小写完全一致 - 确认ASP.NET Core服务端没有手动移除过默认的JSON输入格式化器配置
内容的提问来源于stack exchange,提问作者İbrahim Ethem Bildirici
相关产品推荐
相关产品推荐

