JSON从数据中获取数组键(JSON get array key from data)
我正在尝试从parseJSON函数获取数组键。 我可以得到错误消息 - 努力找到获取数组键的方法。
代码是:
var result = jQuery.parseJSON(response); console.log(result['error']); console.log(result);
我只发送一个密钥,输出看起来像这样:
Object {error: "Please enter a name"}
I'm trying to get the array key from a parseJSON function. I can get the error message just fine - struggling to find a way to get the array key.
code is:
var result = jQuery.parseJSON(response); console.log(result['error']); console.log(result);
I am only sending one key through and the output looks like this:
Object {error: "Please enter a name"}
原文:https://stackoverflow.com/questions/28997426
最满意答案
动态生成HTML的优点
在您的简单示例中,您使用的两个选项中的哪一个实际上并不重要。 但是,如果要创建动态内容,则需要使用像ERB这样的模板系统,如下例所示:
<%= image_tag(@user.logged_in? ? "logged_in.png" : "not_logged_in.png", :class => "login_status") %>
您不能使用静态HTML执行此操作。
性能
在提供HTML之前动态生成这样的HTML显然比服务静态HTML需要更长的时间。 但是,Rails内置了缓存 ,因此您可以轻松控制其影响。
Advantage of dynamically generated HTML
In your simple example, it's actually not important which one of your two options you use. Having a templating system like ERB is however necessary if as you want to create dynamic content, like in this example:
<%= image_tag(@user.logged_in? ? "logged_in.png" : "not_logged_in.png", :class => "login_status") %>
You cannot do this with static HTML.
Performance
Dynamically generating the HTML like this before serving it will obviously take a bit longer than serving static HTML. Rails has caching built in, however, so you can easily control the impact of this.
相关问答
更多-
Rails:Application.html.erb突然停止渲染(Rails: Application.html.erb suddently stopped rendering)[2022-09-09]
您正在控制器中定义initialize方法而不调用super 。 def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] super end You are defining the initialize method in your controller without calling super. def initialize(attributes = {}) @ ... -
感谢Emu在ERB中发布了答案,我发现了一种在haml中执行此操作的方法: :plain $('.modal-body').html('#{j render partial: 'details'}'); // in HAML $('.modal-body').html('<%= j render partial: 'details'%>'); //in ERB Thanks to Emu who posted the answer in ERB I found a way of doing it i ...
-
动态生成HTML的优点 在您的简单示例中,您使用的两个选项中的哪一个实际上并不重要。 但是,如果要创建动态内容,则需要使用像ERB这样的模板系统,如下例所示: <%= image_tag(@user.logged_in? ? "logged_in.png" : "not_logged_in.png", :class => "login_status") %> 您不能使用静态HTML执行此操作。 性能 在提供HTML之前动态生成这样的HTML显然比服务静态HTML需要更长的时间。 但是,Rails内置了缓存 ...
-
html + rails问题(html + rails question)[2023-09-19]
您可以像这样呈现部分: <%= render :partial => 'name_of_partial', :locals => { :some_variable_to_pass_to_partial => "abcd" } %> 这里有一些关于Rails中布局和视图的好信息。 You can render partial like this: <%= render :partial => 'name_of_partial', :locals => { :some_variable_to_pass_to_ ... -
我相信如果你看一下像HTML Imports这样的东西,这可以解决你的问题。 它迎合了极大的灵活性和可重用性。 您只需添加以下内容即可声明部分导入: 到你的HTML页面。 I believe if you look at something like HTML Imports, this could solve your question. It caters for great flexibility and re ...
-
用Java渲染HTML(Rendering HTML in Java)[2022-05-10]
您忘了设置JEditorPane的内容类型。 jep.setContentType("text/html"); You forgot to set the content type of the JEditorPane. jep.setContentType("text/html"); -
你可以使用部分 : 创建一个视图,其文件名以下划线开头,例如:/views/shared/_navbar.html.erb 那么你可以在任何你喜欢的地方渲染这个部分 <%= render :partial => "shared/navbar" %> 在你看来。 请注意,虽然部分文件名在开头有一个下划线,但渲染会声明其名称,省略下划线。 You can use partials: Create a view which its filename starts with an underscore, for ...
-
在rails中呈现HTML文件(Rendering an HTML file in rails)[2022-04-14]
您可以使用render_to_string 。 请看看这里。 http://russbrooks.com/2009/11/18/embed-a-static-hmtl-page-in-a-rails-view You can use render_to_string. Please have a look over here. http://russbrooks.com/2009/11/18/embed-a-static-hmtl-page-in-a-rails-view -
所有公用文件夹内容都可通过“/”访问 您可以使用high_voltage gem生成static_pages All the public folder content is accessible via "/" You can use ...