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
相关产品推荐
相关产品推荐

