title: jquery.post(): how do i honor a redirect from the server?
body: <p>I'm trying my hand at unobtrusive JS, using JQuery in my Ruby On Rails app.</p>
  
  <p>After the user fills out a form, the client-side JQuery code calls:</p>
  
  <pre><code>$.post("/premises", ui.form)
  </code></pre>
  
  <p>I can see the POST hit the server, and I can see the server emit a redirect notice to <a href="http://localhost:3000/users/42" rel="nofollow noreferrer">http://localhost:3000/users/42</a> complete with the data to be displayed.</p>
  
  <p>But the browser page doesn't change.  This doesn't really surprise me -- the whole point of client-side javascript is to control what gets updated -- I get that.  But in this case, I'd like to honor whatever the server replies with.</p>
  
  <p>I tried extending the call to post() based on <a href="https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call">How to manage a redirect request after a jQuery Ajax call</a>:</p>
  
  <pre><code>$.post("/premises", 
         ui.item,  
         function(data, textStatus) {
             if (data.redirect) {
                 // data.redirect contains the string URL to redirect to
                 window.location.href = data.redirect;
             } else {
                 // data.form contains the HTML for the replacement form
                 $("#myform").replaceWith(data.form);
             }
         });
  </code></pre>
  
  <p>... but (among other problems) data.redirect is undefined.  I suspect the real answer is simple, right?  Looking forward to it!</p>