简体   繁体   English

仅当所有代码都在一行上时,AJAX请求才能正常运行

[英]AJAX request only functioning properly when all the code is on one line

I have an AJAX request that gets sent from a controller like so: 我有一个这样的控制器发送的AJAX请求:

in controller: 在控制器中:

  respond_to do |format|
    format.js {render 'show_data'}
  end

show_data.js.erb show_data.js.erb

$('#foo').html("<%= render partial: "foo/show_data" %>");

In partial: _show_data.html.erb 部分:_show_data.html.erb

<table> <tr> <th><%= "Name" %> </th> <th><%= "ID" %> </th> </tr> <tr> <td> <%= "Steve" %> </td> <td> <%= "1"%> </td> </tr> </table>

The above table only gets rendered appropriately when all the code is on one line. 仅当所有代码都在一行上时,上表才会正确呈现。 If i press the return key it doesn't show up inside the target div after the AJAX request has gone through. 如果我按回车键,则在AJAX请求通过后它不会显示在目标div中。 This is not unique to the table tag I've just used it as an example. 这不是表格标记所独有的,我只是以它为例。 Anytime there's more than one line it doesn't show up. 任何时候只要有一行以上就不会显示。

Response when it works: 工作时的响应:

$('#proto').html("<table> <tr> <th>Name </th> <th>ID </th> </tr> <tr> <td> Steve </td> <td> 1  </td> </tr> </table>");

Response when it doesn't work: 不起作用时的响应:

$('#proto').html("<table> <tr> <th>Name </th> <th>Rep ID </th> </tr> <tr> <td> @rep.name </td> <td> @rep.id  </td> </tr> </table>
");

This same system of making AJAX requests works fine in another app of mine. 发出AJAX请求的相同系统在我的另一个应用程序中也可以正常工作。 Any help would be greatly appreciated, thanks a lot! 任何帮助将不胜感激,非常感谢!

Is it so important to have the string in few lines instead of a single, long line? 将字符串分成几行而不是一条长行是否很重要? If so, use something like: 如果是这样,请使用类似以下内容的方法:

$('#proto').html(
       "<table> <tr> <th>Name </th>"
     + "<th>ID </th> </tr>"
     + "<tr> <td> Steve </td>"
     + "<td> 1  </td> </tr> </table>"
);

The above table only gets rendered appropriately when all the code is on one line. 仅当所有代码都在一行上时,上表才会正确呈现。 If i press the return key it doesn't show up inside the target div after the AJAX request has gone through. 如果我按回车键,则在AJAX请求通过后它不会显示在目标div中。

That's because JS doesn't allow text literals to go over multiple lines. 这是因为JS不允许文本文字跨越多行。 (And you should have gotten a corresponding error message in your error console.) (并且您应该在错误控制台中获得了相应的错误消息。)

You can work around that in several ways: 您可以通过以下几种方法解决此问题:

  • Concatenate several one-line literals with + as Kamil already suggested. 正如Kamil所建议的,将多个单行文字与+连接起来。
  • Terminate every line with a \\ character (that way JS allows a text literal to continue on the next line). \\字符终止每一行(这样JS允许文本文字在下一行继续)。
  • Probably easiest if you don't need line breaks in your code output for readability (but still need the line breaks in the value): Replace line breaks in your text with \\n (the actual two characters \\ and n ), so that JS sees something like "Foo\\nBar". 如果您不需要代码输出中的换行符以提高可读性(但仍需要该值的换行符),则可能是最简单的:用\\n (实际的两个字符\\n )替换文本中的换行符,以便使用JS看到类似“ Foo \\ nBar”的信息。 That will still mean a line break in the actual value, but it does not break JS syntax. 那仍然意味着实际值中有换行符,但是并没有破坏JS语法。
  • Remove line breaks altogether (and replace them with a space) if you don't need line breaks in the actual value. 如果您不需要实际值中的换行符,请完全删除换行符(并用空格替换)。

All of those could be done server-side automatically. 所有这些都可以在服务器端自动完成。

Turns out I forgot to include 'escape_javascript' before 'render'. 原来我忘了在“渲染”之前包括“ escape_javascript”。 I'm about to go drown myself in the shower. 我要去洗个澡淹死自己。 Thank you for the answers. 谢谢你的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM