【问题标题】:No console output when using render and jbuilder使用 render 和 jbuilder 时没有控制台输出
【发布时间】:2016-12-23 05:53:44
【问题描述】:

我遇到了一个奇怪的问题,导致控制台在解析后不显示输出

 a= "<%=j render 'api/restaurants/index.json.jbuilder',restaurants: @restaurants %>";
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder',  restaurants: @restaurants %>");
 console.log(json);


a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>";
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>");
console.log("console test");


a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>";
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>");
console.log(a);

上面的代码 sn-ps 不会在控制台中呈现任何内容,但是下面的代码 sn-ps 会给出控制台输出。

a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>";
console.log(a);


 a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>";
 console.log("console test");

似乎解析正在阻止控制台输出任何内容。任何人都知道为什么会发生这种情况以及如何解决它

【问题讨论】:

  • "&lt;%= rende 和其他东西在我看来不像 json。我得到 'Unexpected token

标签: javascript ruby-on-rails json jbuilder


【解决方案1】:

jQuery.parseJSON报道:

传入格式错误的 JSON 字符串会导致引发 JavaScript 异常。

所以,您的 api 没有返回正确的 json。

例子:

$(function () {
  try {
    var a = $.parseJSON("{test: 1}");
    console.log(a);
  } catch(err) {
    console.log('Err: ' + err);
  }
});
&lt;script src="https://code.jquery.com/jquery-1.12.4.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 感谢您帮助我找到了问题。该对象实际上不需要解析
猜你喜欢
  • 1970-01-01
  • 2013-03-14
  • 2018-01-20
  • 2011-06-03
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多