javascript - Laravel 5 Eloquent objects and ajax -
i beginner in javascript , jquery , trying data server, access them , display them without reloading page ajax , laravel 5.
so here do:
my controller:
public function get_media_details(){ $media_id = $_post['media_id']; $media_details = media::find($media_id); print_r ($media_details); }
my js function:
function show_media_detail(media_id){ $.ajax({ url: "get_media_details", type:"post", data: { media_id: media_id }, beforesend: function (xhr) { var token = $('meta[name="csrf_token"]').attr('content'); if (token) { return xhr.setrequestheader('x-csrf-token', token); } }, success:function(data){ console.log(data); }, error:function(){ console.log("no data returned"); } }); }
and result of console.log(data):
"app\media object ( [table:protected] => media [fillable:protected] => array ( [0] => name [1] => feed_url ) [connection:protected] => [primarykey:protected] => id [perpage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => array ( [id] => 1 [name] => techcrunch [description] => breaking technology news , analysis. [link] => http://techcrunch.com/ [feed_url] => http://techcrunch.com/feed/ [thumbnail] => rll0vnskwk3bvttwdthyzagxgnlht9ehpu3qe8xk ) etc...
but know trying access specific value of object, example, name of media.
i tried many things , searched lot, keep failing miserably... appreciated !
thank you
change
print_r ($media_details);
to
return $media_details;
in js file
... success:function(data){ (var x in data){ console.log(data.description); // should output description } ....
Comments
Post a Comment