jquery - Using ruby, Sinatra and Mandrill how can I access individal elements from an api call and print as HTML? -
i want print, html, elements returned json object. ruby code is:
get '/get_template_info' mandrill = mandrill::api.new name = "blah" result = mandrill.templates.info name end
the jquery is:
$(document).ready(function(){ $( ".result" ).on( "click", "#edit_template", function() { $.getjson("/get_template_info?name=blah", function(data) { $.each( data, function( key, value ) { var template_txt = '<p>' + this["code"] + '</p>'; $(".edit_div").append(template_txt); });//end each });//end json });//end click });//end doc
in firebug console, get:
{"slug":"blah","name":"blah","code":"blah message\r\n\r\n<p>and blah how is</p>\r\n<p> hope gets better </p>","publish_code":"blah message\r\n\r\n<p>and blah how is</p>\r\n<p> hope gets better </p>","published_at":"2014-02-06 03:36:04","created_at":"2014-02-05 09:08:06.73429","updated_at":"2014-02-06 03:36:04.28132","publish_name":"blah","labels":["mylabel"],"text":"example text","publish_text":"example text","subject":"this subect","publish_subject":"this subect","from_email":"rich@pchme.com","publish_from_email":"rich@pchme.com","from_name":"rich","publish_from_name":"rich"}
but jquery snippet this["code'] printed, in edit_div, "undefined"!
what's wrong here guys? appreciated. thank you.
inside of callback provide $.each
, code seems want this
same data
, it's same value
. so, if this:
$(document).ready(function(){ $( ".result" ).on( "click", "#edit_template", function() { $.getjson("/get_template_info?name=blah", function(data) { $.each( data, function( key, value ) { console.log(key + ":" + value); }); }); }); });
you get:
slug:blah vm770:2 name:blah vm770:2 code:blah message <p>and blah how is</p> <p> hope gets better </p> vm770:2 publish_code:blah message <p>and blah how is</p> <p> hope gets better </p> vm770:2 published_at:2014-02-06 03:36:04 vm770:2 created_at:2014-02-05 09:08:06.73429 vm770:2 updated_at:2014-02-06 03:36:04.28132 vm770:2 publish_name:blah vm770:2 ...
in case, going straight "code" key, do:
$(document).ready(function(){ $( ".result" ).on( "click", "#edit_template", function() { $.getjson("/get_template_info?name=blah", function(data) { var template_txt = '<p>' + data["code"] + '</p>'; $(".edit_div").append(template_txt); }); }); });
Comments
Post a Comment