jquery - Laravel5: How to get a DB value returned to an Ajax call. -
i'm new ajax , using laravel 5. trying pull db value , have display. however, not getting value in console log.
here ajax call:
$(function(){ $.ajax({ type:'get', url:'product_prices', success:function(price){ console.log('success',price) } }); });
the url: 'product_prices' goes route:
route::get('product_prices', 'quotationcontroller@product_prices'); my controller calling db thus:
public function product_prices(){ $price = db::table('products')->select('price')->where('product_code','=', '123')->get(); json_encode($price); } for testing purposes have hard coded $id of product. in production need able pass $id through url. function. don't know how either.
i know query works, if return $price correct value. poor ajax knowledge prevents me getting console.log or getting $id dynamically used.
many !
json_encode() doesn't print out itself
try
echo json_encode($price); when in doubt can inspect full body response in browser dev tools network. or open url directly in browser
Comments
Post a Comment