调用Amadeus Self Service API发送POST请求遇无响应问题求助
解决Amadeus Upsell API请求无响应问题
排查步骤与修复方案
1. 确认服务器可达性
先通过Postman或curl直接请求 POST http://localhost:2801/flight-offers/upselling:
- 若请求失败,说明服务器未启动或端口配置错误,先确保服务器在2801端口正常运行。
- 若服务器返回错误,查看服务器端日志定位具体问题。
2. 修正前端请求数据格式
前端当前直接将offer_id作为请求体发送,但服务器端期望接收包含flight字段的对象:
// 前端修改请求体部分 body: JSON.stringify({ flight: offer }) // 将offer_id包裹在flight字段中
否则服务器端req.body.flight会是undefined,导致调用Amadeus API时参数错误,无法正常响应。
3. 修复服务器端请求解析与错误处理
- 确保服务器端已启用JSON请求体解析中间件,否则
req.body会为空:
// 在app.post路由前添加 app.use(express.json());
- 优化错误处理逻辑,避免直接返回原始错误对象导致响应异常:
// 服务器端修改catch块 .catch(function (error) { console.error('Amadeus API调用失败:', error); res.status(500).json({ error: error.message || '获取升级报价失败' }); });
4. 处理跨域(CORS)问题
若浏览器控制台出现CORS相关报错,需在服务器端配置CORS中间件:
npm install cors
const cors = require('cors'); app.use(cors()); // 开发环境允许所有跨域请求,生产环境可配置指定域名
5. 修正前端响应解析逻辑
使用ofetch(Nuxt内置请求工具)时,响应已自动解析为JSON对象,无需再调用response.json():
async function submitBrandedOffers() { try { const response = await $fetch( 'http://localhost:2801/flight-offers/upselling', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ flight: offer }), } ); console.log(response); return response; } catch (error) { console.error('请求失败:', error); } }
内容的提问来源于stack exchange,提问作者K Jesus
相关产品推荐
相关产品推荐

