I figured out something, this
"[{"dogname":"Belle","comments":"Yr old female. I was found at a rest stop, some tourist brought me here."},{"dogname":"Dale Evans","comments":"4 month female lab mix. Curious playful puppy."}] ◀"
With the brackets, a response like:
return Response::json($dog); // did not work
But
echo json_encode($dog); // Does work
But only if fetch mode is $sth->fetchAll(\PDO::FETCH_ASSOC);
Whole thing, just a test:
public function testg() {
$dogid = Request::input('somevar');
$sql = "SELECT dogname, comments FROM dc_dogs WHERE dogid < :dogid";
$params = ['dogid' => $dogid];
$sth = DB::getPdo()->prepare($sql);
$sth->execute($params);
$dog = $sth->fetchAll(\PDO::FETCH_ASSOC);
echo json_encode($dog);
}
And the jquery:
$(function () {
$("#testme").click(function (event)
{
event.preventDefault();
alert('hello');
var somevar = '3';
$.ajax({
url: 'testg',
type: 'GET',
data: 'somevar=' + somevar,
dataType: 'json',
success: function (data) {
$.each(data, function (index, item) {
alert(item.dogname)
});
},
error: function (err) {
alert(err);
}
});
});
});
That's when a json array is passed with more that one item. But if a single array, then
return Response::json($dog); // Does work
I cannot figure out why.