samedi 2 juin 2018

Java Script getJSON properly not working in laravel 5.+?

Controller

public function getback(Request $request)
{


    $chart = DB::table('BirthChart_1')
             ->where('FileId', 123)
             ->get();



            foreach ($chart as $key ) {
                $f[] = [$key->k1,$key->k2,$key->k3,$key->k4,$key->k5,$key->k6,$key->k7,$key->k8,$key->k9,$key->k10];

            }

            return response()->json($f);
}

javascript

$(document).ready(function() {
  $.getJSON("/getback", function(data) {
    var items = [];

    $.each(data, function(key, val) {
      items.push(val);
    });
    alert(items);
    alert(items[0]);
    alert(items[1]);

  });
});

http://127.0.0.1:8000/getback Result... [[3,4,null,null,null,5,null,null,null,null]]

http://127.0.0.1:8000 Result

alert(items); // 3,4,,,,5,,,,
alert(items[0]); // 3,4,,,,5,,,,  (here I want 3)
alert(items[1]); // undefined (here ,I want 4)

What is my mistake in here ? or how to correct it properly ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire