You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amadeus API getToken函数返回undefined 求助排查

问题原因

你的getToken函数返回undefined的核心原因是AJAX是异步操作:

  • 你在$.ajax(...).done()的回调函数里return token,这个返回值只属于回调函数本身,不会让外层的getToken函数返回这个token。
  • 而getToken函数本身没有任何顶层的return语句,所以默认返回undefined,自然getDestination(getToken())接收的参数就是undefined。
修复方案

步骤1:改造getToken函数,使其返回Promise

我们需要让getToken返回一个Promise,这样就能在异步请求完成后拿到token:

function getToken(){
    return new Promise((resolve, reject) => {
        var settings = {
            "url": "https://test.api.amadeus.com/v1/security/oauth2/token",
            "method": "POST",
            "timeout": 0,
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            "data": {
                "grant_type": "client_credentials",
                "client_id": "IfOSPsTBNvRkU0ViJhpgMiUhW005PX7a",
                "client_secret": "ATXw6AGIidOEJY7c"
            }
        };

        $.ajax(settings)
        .done(response => {
            const token = response.access_token;
            console.log(token);
            resolve(token); // 将token传递出去
        })
        .fail(error => {
            reject(error); // 处理请求失败的情况
        });
    });
}

步骤2:修改按钮点击逻辑,等待token返回后再调用getDestination

不能直接在onclick里嵌套调用,需要新增一个处理函数:

<button onclick="handleButtonClick()"></button>
function handleButtonClick() {
    getToken().then(token => {
        // 拿到token后再调用getDestination
        getDestination(token);
    }).catch(error => {
        console.error('获取Token失败:', error);
    });
}

步骤3:修复getDestination的Authorization头

你原来的代码里Authorization是写死的,需要改成使用传入的token:

function getDestination(token){
    console.log(token);
    var settings = {
        "url": "https://test.api.amadeus.com/v1/reference-data/locations/pois?",
        "method": "GET",
        "timeout": 0,
        "headers": {
            "Authorization": `Bearer ${token}` // 用传入的token替换写死的值
        },
        data: {
            'latitude' : 41.397158,
            'longitude' : 2.160873,
            'radius' : 2,
        }
    };

    $.ajax(settings).done(response => {
        console.log(response);
    });
}

可选:用async/await简化代码

如果你的运行环境支持ES6+,可以用async/await让代码更简洁:

async function getToken(){
    return new Promise((resolve, reject) => {
        var settings = {
            "url": "https://test.api.amadeus.com/v1/security/oauth2/token",
            "method": "POST",
            "timeout": 0,
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            "data": {
                "grant_type": "client_credentials",
                "client_id": "IfOSPsTBNvRkU0ViJhpgMiUhW005PX7a",
                "client_secret": "ATXw6AGIidOEJY7c"
            }
        };

        $.ajax(settings)
        .done(response => resolve(response.access_token))
        .fail(reject);
    });
}

async function handleButtonClick() {
    try {
        const token = await getToken();
        getDestination(token);
    } catch (error) {
        console.error('获取Token失败:', error);
    }
}

内容的提问来源于stack exchange,提问作者tmiwetmiwtwete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 00:25:20