-3
 $(function () {
    $.ajax({
        url: 'image/show',
        dataType: 'json',
        success: function (data) {
            $.each(function (i, item) {
                alert(item.ratio);
            });
        }
    });
 });

get data back like these:

{"id":2,"ratio":0.66666666666667,"src":"public\/media\/flickr\/13842755234_c8e89150b1_m.jpg"}{"id":3,"ratio":1.0491803278689,"src":"public\/media\/flickr\/13845062983_00fd2bebe2_n.jpg"}{"id":4,"ratio":1.5015015015015,"src":"public\/media\/flickr\/13845347184_24ca8d4e5d.jpg"}

QUESTION: I CAN'T ALERT DATA VIA MY JQUERY CODE.IS ANYTHING GOES WRONG IN MY CODES?

I think maybe these data format goes error.

public function show() {

    $dirname = "public/media/flickr/";
    $images = scandir($dirname);
    $ignore = Array('.',"..");

    foreach ($images as $key=>$curimg) {
        if (!in_array($curimg, $ignore)) {
            $imagefile = $dirname.$curimg;
            list($width,$height) = getimagesize($imagefile);    
            $ratio = $width/$height;
            $data = array(
                    'id' =>$key,
                    'ratio' => $ratio,
                    'src' => $imagefile);
            echo json_encode($data);
        }

    }
}

my php function error. I made a mistake in foreach.

San Chen
  • 25
  • 3

2 Answers2

1

Try,

 alert(data.ratio);

There is no need of .each() here.

Full code,

 $(function () {
    $.ajax({
        url: 'image/show',
        dataType: 'json',
        success: function (data) {
            alert(data.ratio);
        }
    });
 });
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

Your $.each it wrong, it should be:

$.each(data, function(index, item) {
 alert(item.ratio)
})
Slaven Tomac
  • 1,552
  • 2
  • 26
  • 38