title: Laravel4 POST unexplained redirect to GET
body: <p>Another I-did-not-sleep-enough question, I'm sure. I'm posting it as a sacrifice to the Elder God Murphy: as soon as I expose my moronity for all to see, I'm <em>guaranteed</em> to find by myself that answer that otherwise will elude me for hours (<em>by way of further penance, I will then post the answer as well</em>).</p>
  
  <p>I have a HTML form that gets rendered as</p>
  
  <pre><code>&lt;form method="post" id="mysearch" action="/search/?uid=1701"&gt;
      &lt;input id="searchterm" type="text" name="query" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The form <em>can</em> be submitted via jQuery <code>$.POST</code> with url of '/search' and data of <code>{ uid: '1701', query: $('#searchterm').val() }</code> <em>and it works</em>.</p>
  
  <p>If I press ENTER after entering something, and thus override the jQuery submission, the following happens:</p>
  
  <ul>
  <li>a POST is issued to the server, as expected.</li>
  <li>the <code>Route::post('/search', function() {...</code> does <strong>not</strong> get invoked.</li>
  <li>a 301 Moved Permanently is returned</li>
  <li>a GET <a href="https://stackoverflow.com/questions/5008089/when-an-http-post-is-redirected-to-get-what-happens-to-the-body-of-the-post"><em>with search parameters lost</em></a> is issued to the URL specified by the Redirect</li>
  <li>quite obviously, the search fails.</li>
  </ul>
  
  <p>The 301 response looks like something by Laravel4, added explicitly:</p>
  
  <pre><code>HTTP/1.0 301 Moved Permanently
  Date: Thu, 28 Nov 2013 14:05:29 GMT
  Server: Apache
  X-Powered-By: PHP/5.4.20
  Cache-Control: no-cache
  Location: http://development/search?uid=1701
  Connection: close
  Content-Type: text/html
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
      &lt;meta http-equiv="refresh" content="1;url=http://development/search?uid=1701" /&gt;
      &lt;title&gt;Redirecting to http://development/search?uid=1701&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  Redirecting to &lt;a href="Redirecting to http://development/search?uid=1701"&gt;Redirecting to http://development/search?uid=1701&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is not the same as <a href="https://stackoverflow.com/questions/14246557/httpurlconnection-redirects-my-post-request-into-a-get">this question</a>, because there the redirect is expected and it's the answer to <em>that</em> which is undesired. Here it's the redirect itself that is generated for no reason that I can (for now) see.</p>
  
  <p>I suspect that for some reason I'm triggering the "security redirect" described in <a href="https://stackoverflow.com/questions/3882149/trying-to-understand-the-post-redirect-get-design-pattern-implemented-with-php">this other answer</a>, that is not triggered by jQuery (either because it puts everything in the POST while here I've one parameter in the URL and another in the POST, or because jQuery uses XHR).</p>
  
  <p>I had thought it might be a CSRF defense, but that particular route isn't shielded. As a last resource, I'll CSRF-protect the route and add the token to the form, even if it looks a bit like voodoo to me. Something vaguely similar appears to be happening in <a href="https://stackoverflow.com/questions/13865416/why-is-rails-redirecting-my-post-but-not-get">Rails</a>.</p>
  
  <h1>Workarounds</h1>
  
  <p>I've got not one, not two, but <strong>three</strong> workarounds that neatly sidestep the question of <em>why</em> is the above happening:</p>
  
  <ul>
  <li>(most brutal) block the <code>keyUp</code> event in the form.</li>
  <li>redirect the <em>submit</em> event of the form to jQuery</li>
  <li>(most transparent) route the above event to <code>$('#search-button').click()</code></li>
  </ul>
  
  <p>...but I'd like to make without the button altogether (which I could do with jQuery) and <strong>without jQuery altogether</strong>. As well as understand what is happening here. I'm 99% certain I'm missing something obvious.</p>
  
  <h1>Debugging</h1>
  
  <p>I'm now going to <code>grep -r "Redirecting to" *</code> the whole framework source code (I expect to find something in <code>Symfony/Components/HttpFoundation/ResponseRedirect</code>) and doing step-by-step from there.</p>