【问题标题】:Why isnt my axios post request in my backend sending any data back to my external api? -为什么我的后端中的 axios 发布请求没有将任何数据发送回我的外部 api? -
【发布时间】:2021-12-29 13:20:03
【问题描述】:

我正在尝试使用 ajax 调用将数据从客户端发送到我的后端 axios 发布请求,该请求应该将数据发布到外部 api url。但是,当我使用 ajax 调用发回数据时,没有任何内容被发送回服务器。我得到一个 200 的状态码,但没有发送任何数据。如果有人能帮助我理解为什么 axios 不向外部 api 发送数据,将不胜感激!我将在下面包含我认为调试此问题所必需的所有内容

这是我的后端控制器 axios 发布请求和路由,应该从我的前端 ajax 调用接收 req.body 数据

postAttributes: async (req, res) => {
        const { building_number, meter, commodity_tag, train_start, train_end, x,
            auto_ignored_percentage, base_temperature, r2, slope, intercept, std } = req.body

        try {

            const headers = {
                'Content-Type': 'application/json',
               
            }

            const attrdata = {
                'building_number': building_number,
                'meter': meter,
                'commodity_tag': commodity_tag,
                'train_start': train_start,
                'train_end': train_end,
                'x': x,
                'auto_ignored_percentage': auto_ignored_percentage,
                'base_temperature': base_temperature,
                'r2': r2,
                'slope': slope,
                'intercept': intercept,
                'std': std
            }

            const postattributes = process.env.ATTR_POST_API

            const response = await axios.post(postattributes, attrdata, {
                headers: headers

            })
            return res.json(response.data)


        } catch (error) {

            console.error(error)
            return res.json(error)

        }
    }

const router = require('express').Router()
const gatewayController = require('../controllers/apiGatewayModel')

router.post('/postAttributes', gatewayController.postAttributes)

这是前端 ajax 调用,将数据发送回 url '/postAttributes',它应该调用 axios 请求以将数据发送回外部 api url。但是,没有任何内容被发送回 axios。我得到的响应只是一个空对象。

   $.ajax({
                    type: 'POST',
                    url: '/postAttributes',
                    data: JSON.stringify({
                        'building_number': building_number,
                        'meter': meter,
                        'commodity_tag': commodity_tag,
                        'train_start': train_start,
                        'train_end': train_end,
                        'x': x,
                        'auto_ignored_percentage': auto_ignored_percentage,
                        'base_temperature': base_temperature === 0 ? null : base_temperature,
                        'r2': r2,
                        'slope': slope,
                        'intercept': intercept,
                        'std': std
                    })
                }).then(function (response) {
                    console.log(response)
                })

这是应该发送回 axios 的数据。 ajax 调用正在获取数据,它只是没有将其发送回 axios。

这是我从 axios 得到的回复。只是一个空对象,api 不接收任何数据。

【问题讨论】:

  • 您的 post 请求中的 JSON.stringify 是否有效?您正在通过数据发送字符串而不是 json。这可能是 JSON 无效且未发送任何内容的原因。
  • 其他中间件很可能阻碍了正确解构请求正文的能力。您应该验证前端作为请求正文传递的内容。 (我们知道 sent 是什么——但 express 对此做了一些处理)。
  • 我们可以看看你的app.js吗?

标签: javascript node.js ajax axios


【解决方案1】:

我认为这里没有任何错误 - 您的代码假定对外部 API 的 POST 返回的不是204 No Content - 尝试在后端函数中放置一些控制台日志以确保所有内容都被调用。例如)

postAttributes: async (req, res) => {
        console.log(req.body);
        const { building_number, meter, commodity_tag, train_start, train_end, x,
            auto_ignored_percentage, base_temperature, r2, slope, intercept, std } = req.body

【讨论】:

    【解决方案2】:

    我的问题是我没有在前端正确使用 req.body。以前我将键值作为字符串,所以当我将数据从 ajax 发送回 axios 时,它没有重新识别 req.body 参数,因为我将它们作为字符串发送。所以在这里使用它们作为变量解决了我的问题。

     $.ajax({
                        type: 'POST',
                        url: '/postAttributes',
                        data: {
                            building_number: building_number,
                            meter: meter,
                            commodity_tag: commodity_tag,
                            train_start: train_start,
                            train_end: train_end,
                            x: x,
                            auto_ignored_percentage: auto_ignored_percentage,
                            base_temperature: base_temperature === 0 ? null : base_temperature,
                            r2: r2,
                            slope: slope,
                            intercept: intercept,
                            std: std
                        }
                    }).then(function (response) {
                        console.log(response)
         
               })
    

    然后我在后端 axios 调用中使用 JSON.stringify,而不是在前端。现在这一切都正确发送数据。

     const attrdata = JSON.stringify({
                    'building_number': building_number,
                    'meter': meter,
                    'commodity_tag': commodity_tag,
                    'train_start': train_start,
                    'train_end': train_end,
                    'x': x,
                    'auto_ignored_percentage': Number(auto_ignored_percentage),
                    'base_temperature': Number(base_temperature),
                    'r2': Number(r2),
                    'slope': Number(slope),
                    'intercept': Number(intercept),
                    'std': Number(std)
                })
                const postattributes = process.env.ATTR_POST_API
    
                const response = await axios.post(postattributes, attrdata, {
                    headers: headers
    
                })
                
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 2022-07-14
      • 1970-01-01
      • 2021-11-24
      • 2021-09-10
      • 2021-10-05
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多