dongqing6755 2011-03-05 06:48
浏览 72
已采纳

从$ .post()获取数组

I am using:

$.post('fromDB.php', function(data) {
    eval(data);
    console.log(data);
    updateTimer();
});

to get some arrays from php. What php returns:

var todayTimeMinutes = [0, 45, 35, 25, 40, 0, 50, 40, 40, 30, 20];
var todayTimeHours = [0, 8, 9, 10, 10, 11, 11, 12, 13, 14, 15];
var todaySectionName = ["Before School", "Period 1", "Period 2", "Formtime", "Interval", "Period 3", "Period 4", "Lunchtime", "Period 5", "Period 6", "After School"];
console.log("Excecution time: 0.00058889389038086 seconds");

The console.log works fine. When I try to access values from the array inside the success function, it works fine. However, accessing it from updateTimer() does not work, and gives me this message in the chrome debugger:enter image description here

  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2011-03-05 07:00
    关注

    I'm guessing that you are attempting to access todaySectionName within updateTimer(). In that case, the reason you're getting the error is that todaySectionName is not in scope in updateTimer.

    So you either need to define updateTimer as a closure within your success function, or you need to find a different way to pass those values to updateTimer. (Like as arguments.)

    So wherever updateTimer is defined, change its signature to this:

    function updateTimer(todayTimeMinutes, todayTimeHours, todaySectionName) {
        // leave this the same
    }
    

    Then change your success function to this:

    $.post('fromDB.php', function(data) {
        eval(data);
        console.log(data);
        updateTimer(todayTimeMinutes, todayTimeHours, todaySectionName);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题