Rails 远程表单抛出 SyntaxError

发布于 2024-11-09 17:24:52 字数 1292 浏览 0 评论 0原文

我有以下表单:

<%= form_for [@commentable, Comment.new], :remote => true do |f| %>
  <%= f.text_area :body %>
  <%= f.submit "Add your comment" %>
<% end %>

然后是控制器(大量精简到基本部分):

def create
  respond_with(@comment) do |format|
    format.html { redirect_to params[:return_url] }
    format.json { render :layout => !request.xhr? }
  end
end

然后这里是用于处理表单 AJAX 的 javascript:

$('#new_comment')
  .bind('ajax:success', function(evt, data, status, xhr){
    var $this = $(this);

    // Append response HTML (i.e. the comment partial or helper)
    $('#comments ol').append(xhr.responseText);
    $('#comments ol li:last-child').effect("highlight", {}, 3000);

    // Clear out the form so it can be used again
    $this.find('input:text,textarea').val('');

    // Clear out the errors from previous attempts
    $this.find('.errors').empty();
  })
  .bind('ajax:error', function(evt, xhr, status, error){
    // Display the errors (i.e. an error partial or helper)
    $(this).find('.errors').html(xhr.responseText);
  });

表单提交正常并且注释按其应有的方式附加,但 Safari 的 Web Inspector 显示提交评论表单时,我所在页面的第 1 行(这只是文档类型)出现 SyntaxError: Parse error,我不明白为什么。

甚至不知道从哪里开始。

I've got the following form:

<%= form_for [@commentable, Comment.new], :remote => true do |f| %>
  <%= f.text_area :body %>
  <%= f.submit "Add your comment" %>
<% end %>

Then the controller (heavily stripped down to the basic part):

def create
  respond_with(@comment) do |format|
    format.html { redirect_to params[:return_url] }
    format.json { render :layout => !request.xhr? }
  end
end

Then here is the javascript for handling the form AJAX:

$('#new_comment')
  .bind('ajax:success', function(evt, data, status, xhr){
    var $this = $(this);

    // Append response HTML (i.e. the comment partial or helper)
    $('#comments ol').append(xhr.responseText);
    $('#comments ol li:last-child').effect("highlight", {}, 3000);

    // Clear out the form so it can be used again
    $this.find('input:text,textarea').val('');

    // Clear out the errors from previous attempts
    $this.find('.errors').empty();
  })
  .bind('ajax:error', function(evt, xhr, status, error){
    // Display the errors (i.e. an error partial or helper)
    $(this).find('.errors').html(xhr.responseText);
  });

The form submits fine and the comment gets appended as it should, but Safari's Web Inspector shows a SyntaxError: Parse error on line 1 (which is just the doctype) of whatever page I'm on when the comment form is submitted, and I can't figure out why.

Not even sure where to start with this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

晨光如昨 2024-11-16 17:24:52

我收到了这个错误,这是因为远程 ajax 调用需要 JavaScript,而我返回的是 html。 .ajax 调用默认为 dataType 'script',因此当它获得 ajax 结果时,它会尝试对其进行评估,并且任何 <<标签导致了错误。

我通过将视图从 .js.erb 更改为 .html.erb 并添加 'data-type' => 消除了错误'html' 到包含 :remote =>; 的链接正确。

在某些情况下,让表单本身成为 JavaScript 片段可能是有意义的 - 类似于:

$('#container').replace("<%= escape_javascript(render(...)) %>");

然后您可以从链接中省略数据类型。

有关数据类型的更多信息 在这里

I have gotten that error and it was because the remote ajax call was expecting JavaScript and I was returning html. The .ajax call was defaulting to dataType 'script' so when it got the ajax result, it tried to evaluate it and any < tags caused the error.

I got rid of the error by changing the view from .js.erb to .html.erb and adding 'data-type' => 'html' to the link that contained :remote => true.

In some cases, it may make sense to have the form itself be a JavaScript snippet - something like:

$('#container').replace("<%= escape_javascript(render(...)) %>");

Then you can omit the data-type from the link.

More information about data-type here.

青瓷清茶倾城歌 2024-11-16 17:24:52

尝试将 bind 调用与 new_comment 引用放在同一行:

$('#new_comment').bind('ajax:success', function(evt, data, status, xhr){

Try putting your bind call on the same line as the new_comment reference:

$('#new_comment').bind('ajax:success', function(evt, data, status, xhr){
原谅过去的我 2024-11-16 17:24:52

我认为您正在查看错误的第 1 行 - 您是否包含 JavaScript 文件,这可能意味着其中之一的第 1 行。它也可能意味着发回内容的第一行。

说实话 - 尝试 Chrome 并打开调试,Chrome 更擅长说出哪个文件给它带来了问题以及在哪里。

无论如何,您不需要将所有 JQuery 放入文件中 - 如果安装了正确的 gem,您可以在视图中编写响应 - 不确定这是否有帮助。

I think you're looking at the wrong line 1 - are you including your JavaScript files, it might mean line 1 of one of them. It might also mean line 1 of what's being sent back.

To be honest - try Chrome and switch on the debugging, Chrome is much better at saying which file gave it the problem and where.

You don't need to put all the JQuery in your file anyway - you can write a response in a view if you have the right gems installed - not sure if that would help.

百合的盛世恋 2024-11-16 17:24:52

您的页面上是否有一个包含“错误”类的 div?我遇到了非常相似的问题,并在寻找解决方案时发现了这个问题。

我的问题是我们试图写回一个不存在的 div(因为页面上有 except .empty?)。当我们解决这个问题时,成功回调就起作用了。

Do you have a div on the page with class "errors"? I was having a very similar problem, and found this question while searching for a solution.

My issue was that we were trying to write back to a div that didn't exist (because of an unless .empty? on the page). When we solved that, the success callback worked.

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