我正在嘗試使用 ajax 呼叫將資料從我的客戶端發送到我的后端 axios post 請求,該請求應該將資料發布到外部 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)
這是將資料發送回 url '/postAttributes' 的前端 ajax 呼叫,它應該呼叫 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 不接收任何資料。

uj5u.com熱心網友回復:
我不認為這里有任何錯誤 - 您的代碼假設對外部 API 的 POST 回傳的不是 a 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
uj5u.com熱心網友回復:
我的問題是我沒有在前端正確使用 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
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360162.html
標籤:javascript 节点.js 阿贾克斯 公理
