1

我在我的开发环境中工作。我想要的行为:我在模态对话框上有一个表单。用户单击提交并将数据发送到我的“问题”控制器。发送了一些电子邮件,然后刷新对话框的内容以显示“消息发送成功”。很基本。问题是我无法正确设置标题,因此 rails 将数据处理为 html,而不是 javascript。电子邮件出去了,但 rails 返回 406 错误。这是我的代码:

_social.html.erb

    <div class='jqmWindow notices' id="emailModal">
      <h3>Email a link to this poll</h3>
      <p>You are emailing a link to the following poll:<br><strong><%=@question.text%></strong></p>
      <%= form_tag(:controller=>'questions', :action=>'email' , :method => "post",  :id=>"emailForm") do %>
              <%= label_tag(:emails_label, "Enter up to 20 email addresses separated by commas:" )%>
          <%= text_area_tag(:emails, "",:size => "60x6")%>
          <%= hidden_field_tag(:question_id, @question.id )%>
              <%= label_tag(:email_msg_label, "Enter a brief message to accompany your link:" )%>
          <%= text_area_tag(:email_msg, "",:size => "60x4")%><br>

          <%= submit_tag ( "Submit")%>
        <%end%>
    </div>

...

<script>

$(document).ready(function(){
  $('#shareModal').jqm();
  $('#emailModal').jqm();
  $.ajaxSetup({
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Accept", "text/javascript")
    }
  });


  $(document).ajaxSend(function(e, xhr, options) {
    var token = $("meta[name='csrf-token']").attr("content");
    xhr.setRequestHeader("X-CSRF-Token", token);
  });

...

  $('#emailForm').submit(function(){
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
  });
});

</script>

questions_controller.rb

 def email
    question = Question.find(params[:question_id])
    emails = params[:emails].split(/, /)
    emails.each do |recipient|
      GenericMailer.share(question, current_user.email, recipient, params[:email_msg]).deliver
    end
    respond_to do |format|
      format.js
    end


  end

email.js.erb(现在,这段代码永远不会被执行!)

alert("hello!");
$(#emailModal).html("Email sent successfully!");

这是标题:

    Request URL:http://127.0.0.1:3000/questions/emailForm/email?method=post
    Request Method:POST
    Status Code:406 Not Acceptable
    Request Headersview source
    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
    Accept-Encoding:gzip,deflate,sdch
    Accept-Language:en-US,en;q=0.8
    Cache-Control:max-age=0
    Connection:keep-alive
    Content-Length:172
    Content-Type:application/x-www-form-urlencoded
__utma=96992031.1133215771.1353171766.1354190632.1354240040.26; __utmb=96992031.69.10.1354240040; __utmc=96992031; __utmz=96992031.1353379538.10.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=http://127.0.0.1:3000/questions/5
    Host:127.0.0.1:3000
    Origin:http://127.0.0.1:3000
    Referer:http://127.0.0.1:3000/questions/1
    User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11

提前感谢您提供的任何帮助!

4

3 回答 3

1

感谢所有回复的人。事实证明,ajax 代码甚至从未被执行过。这是一个轨道问题。这是罪魁祸首:

<%= form_tag(:controller=>'questions', :action=>'email' , :method => "post",  :id=>"emailForm") do %>

id 被附加到请求中,而不是作为 css id 添加到表单标记中。因此 jQuery 选择器没有捕获表单提交。正确的代码:

<%= form_tag({:controller=>'questions', :action=>'email' }, {:id=>"emailForm"}) do %>

干杯....

于 2012-12-01T14:44:58.387 回答
0

看来您的 ajaxSetup 可能不正确,JS 的官方 MIME 类型是application/javascript

在标题中,您可以看到Accept:字符串中没有 JS 的任何内容。首先尝试调用.js附加的 URL 以排除您的 Rails 控制器。

然后在浏览器检查器中确保application/javascript在接受列表中排在第一位。

于 2012-11-30T12:32:58.173 回答
0

您是否调试了 email.js.erb 中的 javascript/jquery 代码?406 错误通常表示您的 js 代码中的拼写错误(这对于错误代码的含义并不明显)。

至少,在您的示例中,第二行有错字,缺少单引号:

 $('#emailModal').html("Email sent successfully!"); 
于 2012-11-30T21:47:17.047 回答