javascript - Axios POST 请求 : Content-type is set, 但在 spring 内它是空的

标签 javascript java spring

我使用 Axios'Content-Type': 'multipart/form-data' 以 POST 请求发送视频,但在 Spring 中我看到以下错误:

"[org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]" 

Axios 代码:


async function uploadPost() {
    const type = 'video/mp4';

    let data = new FormData();
    const videoUri = imagePreview;

    data =
      ('video',
      {
        name: 'mobile-video-upload',
        type,
        videoUri,
      });
    axios({
      method: 'post',
      url: 'http://localhost:8080/post/upload',
      body: data,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }).catch(function(error) {
      console.log(error);
    });


Spring内部的函数:

   @CrossOrigin
   @ResponseBody
   @RequestMapping(value = "/post/upload",  method = RequestMethod.POST, consumes = "multipart/form-data")
   public String create(@RequestParam("file") MultipartFile file) {

提前致谢。

最佳答案

假设 Spring 代码正确,请尝试使用 Axios 进行细微更改。

const axios = require('axios')
const qs = require('querystring')
....... other code .....

const config = {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
}

axios.post(url, qs.stringify(requestBody), config)
  .then((result) => {
    // Do somthing
  })
  .catch((err) => {
    // Do somthing
  })

关于javascript - Axios POST 请求 : Content-type is set, 但在 spring 内它是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576421/

相关文章:

javascript - 在 ajax 成功中无法检测到来自 mysql 的 json_encode 结果

javascript - 添加 Iframe 时 Knockout.js 数据绑定(bind)不起作用

java - 如何使用 "interface builders"构建跨 IDE java GUI?

java - 有条件的类导入

spring - 重定向后如何保留请求参数?

spring - 如何在H2数据库中使用MySQL特有的函数?

java - Spring 集成 - 如何在不轮询的情况下检查文件目录?

javascript - 如何调用onclick事件,重新启用功能|仅限纯 JS

javascript - Laravel 5.5 - CORS GET 请求更改为 OPTIONS

java - 如何实现服务器端对数据表的排序