dongyoulou4829 2014-12-11 17:05
浏览 273
已采纳

使用foreach函数时的javascript异常

I am trying to parse an associated array returned from a PHP functoin like this:

$.ajax({
        type: "GET",
        url: "../controllers/Checkpoint.php/getAllCheckpoints"
    }).done(function( result ) {
        result.foreach(function(index, value){
            alert(index + "  "+ value);
        });
    }).error(function(){

    });

i return the array like this in php

foreach($result as $row) {
            $checkpoints[$row['ID']] = $row['Name'];
        }
        return json_encode($checkpoints);

the result of that array reading from the chrome F12 console is

{"1":"check point 1","2":"check point 2"}

I got this error in the javascript code:

ReferenceError: forEach is not defined

could you help me please

  • 写回答

4条回答 默认 最新

  • dougongnan2167 2014-12-11 17:31
    关注

    I would suggest using the jQuery.each() generic iterator function in combination with the javascript JSON.parse() to accomplish what you are attempting to do.

    jQuery.each()

    JSON.parse

    .done(function( result ) {
        checkpoints = JSON.parse(result);
        $.each( checkpoints, function( index, value) {
             alert(index + "  "+ value);
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b