I want to print, as HTML, elements from a returned JSON object. The ruby code is:
我想以HTML格式打印返回的JSON对象中的元素。 ruby代码是:
get '/get_template_info' do
mandrill = Mandrill::API.new
name = "blah"
result = mandrill.templates.info name
end
The jQuery is:
jQuery是:
$(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 the firebug console, this is what I get:
在firebug控制台中,这是我得到的:
{"slug":"blah","name":"blah","code":"blah is the message\r\n\r\n<p>and blah is how it is</p>\r\n<p> I hope it gets better soon </p>","publish_code":"blah is the message\r\n\r\n<p>and blah is how it is</p>\r\n<p> I hope it gets better soon </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 is a subect","publish_subject":"this is a subect","from_email":"rich@pchme.com","publish_from_email":"rich@pchme.com","from_name":"Rich","publish_from_name":"Rich"}
but the jQuery snippet this["code'] is printed, in the edit_div, as "undefined"!
但是这个[“code”]的jQuery片段在edit_div中打印为“undefined”!
What's wrong here guys? All help appreciated. Thank you.
这家伙有什么不对?所有帮助赞赏。谢谢。
1 个解决方案
#1
0
Inside of the callback you provide to $.each
, your code seems to want this
to be the same as data
, but actually it's the same as value
. So, if you do this:
在$ .each中提供的回调内部,您的代码似乎希望它与数据相同,但实际上它与值相同。所以,如果你这样做:
$(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 will get:
你会得到:
slug:blah VM770:2
name:blah VM770:2
code:blah is the message
<p>and blah is how it is</p>
<p> I hope it gets better soon </p> VM770:2
publish_code:blah is the message
<p>and blah is how it is</p>
<p> I hope it gets better soon </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 your case, you are going straight for the "code" key, so you could just 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);
});
});
});
#1
0
Inside of the callback you provide to $.each
, your code seems to want this
to be the same as data
, but actually it's the same as value
. So, if you do this:
在$ .each中提供的回调内部,您的代码似乎希望它与数据相同,但实际上它与值相同。所以,如果你这样做:
$(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 will get:
你会得到:
slug:blah VM770:2
name:blah VM770:2
code:blah is the message
<p>and blah is how it is</p>
<p> I hope it gets better soon </p> VM770:2
publish_code:blah is the message
<p>and blah is how it is</p>
<p> I hope it gets better soon </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 your case, you are going straight for the "code" key, so you could just 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);
});
});
});