Hi. I am developing a web application with Rails.
I need to pass data to JavaScript, so let Rails generate JSON on load like,
- <%= content_tag "div", id: "json", data: {script: @video.scripts } do %>
- <% end %>
It generates HTML,
- <div data-script="[{"created_at":"2012-09-03T11:17:37+09:00","endp":"45.3","id":80,"startp":"30.1","text":"generated in console for test","updated_at":"2012-09-03T11:17:37+09:00","video_id":11},{"created_at":"2012-09-03T13:32:02+09:00","endp":"86.0","id":81,"startp":"60.6","text":"another generation from console","updated_at":"2012-09-03T13:32:02+09:00","video_id":11}]" id="json">
- </div>
It's messy but a JSON object is stored properly.
Then I tried to get the data using jQuery
- var json = $('#json').data();
- $.getJSON( json, function(data) {
- console.log(data);
- alert(data);
- });
But this gives an error
I understand the first argument of $.getJSON is URL, but I am having trouble getting the set variable, "json".
Could anyone help me out for this? If there is a better way, I would like that too.
soujiro0725