【问题标题】:Javascript File object missing from body正文中缺少 Javascript 文件对象
【发布时间】:2022-12-31 02:46:44
【问题描述】:

当我在前端执行请求时,我有一个有效的目标文件,其中包含数据。 但是当我在 express 后端收到正文时,文件对象是空的。

 let body = {
            metadata: {
            date: date, 
            location: location, 
            description: description, 
            file: file
            }
        }


    const headers = {'body': JSON.stringify(body)}

  
    axios.get('/api/xummMint', {headers}).then( (res) => { 


      console.log("xumm data coming");
      console.log(res)

      setData({xummData: res});

    })

当我 console.log(req.headers.body) 我收到一个空文件对象,即使该文件在前端有数据:

{"metadata":{"date":"2022-12-30","location":"asf","description":"asdf","file":{}}}

我尝试使用 fetch 并添加 content-Type: application/json 但结果仍然相同。谁能帮忙?

【问题讨论】:

    标签: node.js express file axios


    【解决方案1】:

    您可能打算将其作为带有 JSON 正文的 POST 请求发送,而不是作为带有名为“body”的标头的 GET 请求发送

    axios.post(
        '/api/xummMint',
        body, 
        {headers: {'Content-Type': 'application/json'}}
    ).then(res => {
        console.log("xumm data coming");
        console.log(res);
    
        setData({xummData: res});
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多