前端页面出现415 Unsupported Media Type异常

如图我在向后端发送post请求时候,出现了415异常

我使用postman测试,没有问题,但是使用前端页面发送请求就不行。后来发现报错的详细信息是:

timestamp: 1693552677600, status: 415, error: "Unsupported Media Type",…}
error
: 
"Unsupported Media Type"
exception
: 
"org.springframework.web.HttpMediaTypeNotSupportedException"
message
: 
"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"
path
: 
"/resource-dispatch-service/v1/scheduling_task_schemess"
status
: 
415
timestamp
: 
1693552677600

显示不支持'application/x-www-form-urlencoded;charset=UTF-8'这种格式,这里说明前端像后端发送的是这种格式的数据,而我的后端需要接收的是'Content-Type': 'application/json;charset=UTF-8',也就是json格式的数据。因此报错。那为啥我像后端传递的数据变了呢?经过排查发现,是前端页面的拦截器出现了问题,我向后端发送的请求,经过拦截器拦截后,没有走'Content-Type': 'application/json;charset=UTF-8'这个逻辑,所以报错了。

 if (
        config.url.indexOf('****service') !== -1 ||
        config.url.indexOf('****service') !== -1 ||
      ) 
        {
        config.headers = {
          'Content-Type': 'application/json;charset=UTF-8',
        };
        return config;
        
      } 
else if (config.url === '/url/******') {
        config.headers = {
          'Content-Type': 'multipart/form-data; boundary=<calculated when request is sent>',
        };
        return config;
      } else {
        config.headers = {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        };
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值