Note that there are some explanatory texts on larger screens.

plurals
  1. PO406 error from rails jquery ajax
    text
    copied!<p>I'm working in my development environment. The behavior I want: I have a form on a modal dialog. The user clicks submit and data is sent to my "questions" controller. Some emails are sent and then the contents of the dialog are refreshed to say "Message sent successfully." Pretty basic. The problem is that I can't set the headers properly so rails is processing the data as html, not javascript. The emails go out but rails returns a 406 error. Here is my code:</p> <p><strong>_social.html.erb</strong></p> <pre><code> &lt;div class='jqmWindow notices' id="emailModal"&gt; &lt;h3&gt;Email a link to this poll&lt;/h3&gt; &lt;p&gt;You are emailing a link to the following poll:&lt;br&gt;&lt;strong&gt;&lt;%=@question.text%&gt;&lt;/strong&gt;&lt;/p&gt; &lt;%= form_tag(:controller=&gt;'questions', :action=&gt;'email' , :method =&gt; "post", :id=&gt;"emailForm") do %&gt; &lt;%= label_tag(:emails_label, "Enter up to 20 email addresses separated by commas:" )%&gt; &lt;%= text_area_tag(:emails, "",:size =&gt; "60x6")%&gt; &lt;%= hidden_field_tag(:question_id, @question.id )%&gt; &lt;%= label_tag(:email_msg_label, "Enter a brief message to accompany your link:" )%&gt; &lt;%= text_area_tag(:email_msg, "",:size =&gt; "60x4")%&gt;&lt;br&gt; &lt;%= submit_tag ( "Submit")%&gt; &lt;%end%&gt; &lt;/div&gt; ... &lt;script&gt; $(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; }); }); &lt;/script&gt; </code></pre> <p><strong>questions_controller.rb</strong></p> <pre><code> 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 </code></pre> <p><strong>email.js.erb (right now, this code never gets executed!)</strong></p> <pre><code>alert("hello!"); $(#emailModal).html("Email sent successfully!"); </code></pre> <p><strong>and here are the headers:</strong></p> <pre><code> 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 </code></pre> <p>Thanks in advance for any help you can provide!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload