JQuery 超级基础知识。当我看到 data 变量时,为什么无法访问 data.html?
我正在使用 JQuery-ujs + Rails。我对问题的解决方案有 90% 的信心,但被难住了:
- 我在屏幕上看到成功
- 我看到数据
- 我看不到 data.html =>为什么??我怎样才能访问这个?我在日志下面的哈希中看到它。
这是我正在处理的事情:
$(function(){
$('#add-mod-form').bind('ajax:success', function(evt, data, status, xhr){
alert("Success!");
console.log(data.success);
console.log(data.html);
console.log(xhr.responseText);
console.log(evt);
console.log(data);
console.log(status);
console.log(xhr);
//works!
$('#page').append(xhr.responseText);
//does not work!
$('#page').append(data.html);
});
});
这是我的日志:
undefined
undefined
{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}
Object { type="ajax:success", timeStamp=1310160257936, more...}
{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}
success
Object { readyState=4, responseText="{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}", more...}
I'm working with JQuery-ujs + Rails. and I'm 90% to the problem's solution but stumped:
- I see success on screen
- I see data
- I can't see data.html => why?? How can I access this? I see it in the hash below in my logs.
Here is what I am dealing with:
$(function(){
$('#add-mod-form').bind('ajax:success', function(evt, data, status, xhr){
alert("Success!");
console.log(data.success);
console.log(data.html);
console.log(xhr.responseText);
console.log(evt);
console.log(data);
console.log(status);
console.log(xhr);
//works!
$('#page').append(xhr.responseText);
//does not work!
$('#page').append(data.html);
});
});
Here is my logs:
undefined
undefined
{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}
Object { type="ajax:success", timeStamp=1310160257936, more...}
{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}
success
Object { readyState=4, responseText="{"success":true,"html":"<div class=\"extrafield\" id=\"extrafield_73\">\n<div class='trash_can'>Nala <a href=\"#\" data-confirm=\"Are you sure you want to delete Nala?\" data-href=\"/delete_extra_field/73\"><img alt=\"Trash\" src=\"http://\" /></a></div>\n</div>\n"}", more...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的
data
是 json string 而不是对象。你应该这样做:注意
$.parseJSON
从 json 字符串解码为对象。希望这有帮助。干杯
It's because your
data
is a json string and not an object. You should do:Note that
$.parseJSON
decodes from json string to object.Hope this helps. Cheers
html也是一种方法。使用 data.html() 代替。
also html is a method. Use data.html() instead.