[英]Axios POST request returns 404 error when sending to Node.js API
[英]POST request is working using POSTMAN, but when I'm trying to call the same link using Axios from browser(UI) it's showing 404 error | node.js
Api 致電 -
useEffect(() => {
ticketConfirmationApi.getBookedPassengersDetails(transactionId)
.then(response => response.data)
.then(data => {
// console.log("Data from mongoDB PassengeDetails")
// console.log(data)
setBookedPassengers(data)
})
ticketConfirmationApi.getBookingHistory(transactionId)
.then(response => response.data)
.then(data => {
// console.log("Data from mongoDB History")
// console.log(data)
setBookingHistory(data)
})
}, [])
API -
import axios from 'axios'
export function getBookedPassengersDetails(transactionId) {
let apiUrl = 'http://localhost:8080/passengers/get-passengers/tid'
return axios.post(apiUrl, transactionId)
}
export function getBookingHistory(transactionId) {
let apiUrl = 'http://localhost:8080/booking-history/get-history/tid'
return axios.post(apiUrl, transactionId)
}
發布請求 -
它保存在名為passengers.js的js文件中
.post('/get-passengers/tid', async (req, res, next) => {
PassengerDetails.find({ transactionId: req.body.transactionId }, (err, foundData) => {
if (err) {
console.log(err)
res.status(500).send()
} else {
if (foundData.length === 0) {
res.status(404).send("No data available")
} else {
console.log(foundData)
res.json(foundData)
}
}
})
})
它保存在名為 booking-history.js 的 js 文件中
.post("/get-history/tid", (req, res, next) => {
try{
BookingHistory.find({ transactionId: req.body.transactionId }, (err, foundData) => {
if (err) {
console.log(err)
res.status(500).send()
} else {
if (foundData.length === 0) {
res.status(404).send("No data available")
} else {
console.log(foundData)
res.json(foundData)
}
}
})
}catch (err) {
console.log("Some error occured : " + err)
}
})
我正在嘗試做的是使用“transactionId”獲取詳細信息,但代碼在使用 postman 時有效,這表明提供的鏈接很好,不知道為什么通過瀏覽器調用時它不起作用(UI) .
已解決的問題:在 API 部分中,參數“transactionId”應為 Object 類型,即 - {transactionId}
import axios from 'axios'
export function getBookedPassengersDetails(**{transactionId}**) {
let apiUrl = 'http://localhost:8080/passengers/get-passengers/tid'
return axios.post(apiUrl, transactionId)
}
export function getBookingHistory(**{transactionId}**) {
let apiUrl = 'http://localhost:8080/booking-history/get-history/tid'
return axios.post(apiUrl, transactionId)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.