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

jQuery $.get请求返回Ajax response为undefined问题求助

问题修复方案
  • 核心错误:jQuery $.get 回调参数顺序颠倒
    jQuery的$.get请求成功后的回调函数,入参顺序为(返回的数据, 状态文本, jqXHR对象),你代码中将参数写反,实际取到的price是状态文本字符串,无法从中读取currentPrice属性,因此得到undefined。
  • 错误捕获逻辑错误
    你当前写的回调仅会在请求成功时触发,不会接收到错误信息,错误需要通过.fail()方法单独捕获。

修复后代码

///////////// 弹窗显示函数 //////////////////
// 开始//

$('#button1').on('click', function() {
    // 展示弹窗
    $('#openPopup').show();
    // 获取交易代码并转为大写,展示在弹窗中
    const symbolID = document.getElementById("symbolSearch").value.toUpperCase();
    document.getElementById("currentSymbol").innerHTML = symbolID;

    $.get("http://localhost:5000/placeOrder", function(price, status) {
        // 第一个参数为后端返回的完整数据
        console.log(price.currentPrice);
        var cPrice = price.currentPrice; 
        console.log(cPrice);
        document.getElementById("currPrice").innerHTML = cPrice;
    }).fail(function(err) {
        // 请求失败时打印错误
        console.log(err);
    });

// 执行获取价格的函数
//userAction();
});

调试建议

如果修改后仍有问题,可以在回调第一行添加console.log(price)打印完整的返回数据结构,确认后端返回的数据中确实存在currentPrice字段即可快速定位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 17:51:00