javascript - Accessing JSON parsed object -


so here js code:

$(document).ready(function() {     $.ajax({         url: "/",         type:"post",         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("error!!!!");         }     });   }); 

and in console:

object { all_creations: "[{"id":"2","user_id":"2","title":"d…" } 

but i'd like:

console.log(data.user_id) 

but return nothing..

same for:

console.log(data['all_creations'].user_id) console.log(data['all_creations'][0].user_id) console.log(data[0].user_id) 

...

i using laravel5 btw , json object return tojson() function. (if help)

i know question has been answered millions times reason cannot work on project... not pro in javascript or related json. ajax, json remain me source of intense pain. hope 1 day... ^^

if anything, rough guide how can access data:

object { all_creations: "[{"id":"2","user_id":"2","title":"d…" } ^^^^^^^^                                                       ^ 

the marked areas indicate data standard object, has been parsed.

object { all_creations: "[{"id":"2","user_id":"2","title":"d…" }          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

the inner portion says data.all_creations property contains string value.

object { all_creations: "[{"id":"2","user_id":"2","title":"d…" }                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

the string value seems contain json encoded value, need parse first:

var creations = json.parse(data.all_creations); 

then, string value can see contains array object first element.

alert(creations[0].user_id) // 2 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -