JQuery 超级基础知识。当我看到 data 变量时,为什么无法访问 data.html?

发布于 2024-11-19 07:35:36 字数 1750 浏览 3 评论 0原文

我正在使用 JQuery-ujs + Rails。我对问题的解决方案有 90% 的信心,但被难住了:

  1. 我在屏幕上看到成功
  2. 我看到数据
  3. 我看不到 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:

  1. I see success on screen
  2. I see data
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-11-26 07:35:36

这是因为您的 data 是 json string 而不是对象。你应该这样做:

  $('#add-mod-form').bind('ajax:success', function(evt, data, status, xhr){
      data = $.parseJSON(data); //This converts 'data' from json string to object
      alert("Success!");      
      //The rest of your code
  });

注意 $.parseJSON 从 json 字符串解码为对象。

希望这有帮助。干杯

It's because your data is a json string and not an object. You should do:

  $('#add-mod-form').bind('ajax:success', function(evt, data, status, xhr){
      data = $.parseJSON(data); //This converts 'data' from json string to object
      alert("Success!");      
      //The rest of your code
  });

Note that $.parseJSON decodes from json string to object.

Hope this helps. Cheers

就是爱搞怪 2024-11-26 07:35:36

html也是一种方法。使用 data.html() 代替。

also html is a method. Use data.html() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文