我收到内部服务器错误 (500) 错误 当我使用带有正确参数的航班搜索 api 时。如果我的代码有任何问题,任何人都可以解释我。 我尝试在我的项目中进行航班搜索 api 集成,我想知道服务器端或我是否有任何问题。
const handleSubmit = async (e) => {
e.preventDefault();
try {
const fromIATA = formData.from_inp.split('(')[1].replace(')', '');
const toIATA = formData.to_inp.split('(')[1].replace(')', '');
const params = {
originLocationCode: fromIATA,
destinationLocationCode: toIATA,
departureDate: formData.depart_inp,
adults: 1, // Assuming default value for adults
children: 1 // Adjust for child passengers
};
// Conditionally add the returnDate if provided
if (formData.return_inp) {
params.returnDate = formData.return_inp;
}
// Fetch access token
const tokenResponse = await axios.post('https://test.api.amadeus.com/v1/security/oauth2/token', {
grant_type: 'client_credentials',
client_id: 'fAAnCC012DHFdZXasXhJDmArGCxy9AUG',
client_secret: 'g37ICvXMDNjSChef'
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const accessToken = tokenResponse.data.access_token;
// Fetch flight offers
const response = await axios.get('https://test.api.amadeus.com/v2/shopping/flight-offers', {
params: params,
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const apiResponse = response.data;
console.log('API Response:', apiResponse);
if (Array.isArray(apiResponse.data)) {
setFlightDetails(apiResponse.data);
setError(''); // Clear any previous errors
} else {
setError('No flight details match your search.');
setFlightDetails([]); // Clear previous flight details
}
} catch (error) {
console.error('Error fetching flight offers:', error);
setError(`Error fetching flight details: ${error.message}`);
setFlightDetails([]); // Clear previous flight details
}
};
此错误表明服务器端出现问题,但它没有提供导致问题的具体详细信息。 以下是您应该做的一些事情:
不然很难解决:)