Laravel4 POST无法解释重定向到GET

问题描述 投票:4回答:1

我确信,另一个我没有睡觉的问题。我把它作为牺牲给老上帝墨菲发布:一旦我揭露了我的所有人看到的蠢事,我保证会自己找到答案,否则我将躲过几个小时(通过进一步的忏悔,然后我会发布答案)。

我有一个HTML表单,呈现为

<form method="post" id="mysearch" action="/search/?uid=1701">
    <input id="searchterm" type="text" name="query" />
</form>

表单可以通过jQuery $.POST提交,其中包含'/ search'的网址和{ uid: '1701', query: $('#searchterm').val() }的数据,并且可以使用。

如果我在输入内容后按ENTER键,从而覆盖jQuery提交,则会发生以下情况:

  • 按预期方式向服务器发出POST。
  • Route::post('/search', function() {...没有被调用。
  • a返回301 Moved Permanently
  • 将GET with search parameters lost发布到Redirect指定的URL
  • 很明显,搜索失败了。

301响应看起来像Laravel4的内容,明确补充说:

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

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="refresh" content="1;url=http://development/search?uid=1701" />
    <title>Redirecting to http://development/search?uid=1701</title>
</head>
<body>
Redirecting to <a href="Redirecting to http://development/search?uid=1701">Redirecting to http://development/search?uid=1701</a>
</body>
</html>

这与this question不同,因为重定向是预期的,它是不受欢迎的答案。这是重定向本身,无缘无故地生成(现在)。

我怀疑由于某种原因我触发了this other answer中描述的“安全重定向”,这不是由jQuery触发的(因为它将所有内容放在POST中,而在这里我在URL中有一个参数而在POST中有另一个参数,或者因为jQuery使用XHR)。

我原本以为它可能是一个CSRF防御,但那个特定的路线没有被屏蔽。作为最后一个资源,我将CSRF保护路由并将令牌添加到表单中,即使它看起来有点像伏都教给我。在Rails似乎发生了一些模糊相似的东西。

Workarounds

我没有一个,而不是两个,而是三个解决方案,巧妙地回避了为什么上面发生的问题:

  • (最残酷的)阻止形式的keyUp事件。
  • 将表单的submit事件重定向到jQuery
  • (最透明的)将上述事件路由到$('#search-button').click()

...但我想完全没有按钮(我可以用jQuery做)并且完全没有jQuery。同时了解这里发生了什么。我99%肯定我错过了一些明显的东西。

Debugging

我现在要去grep -r "Redirecting to" *整个框架源代码(我希望在Symfony/Components/HttpFoundation/ResponseRedirect中找到一些东西)并从那里一步一步地做。

php jquery symfony redirect laravel-4
1个回答
14
投票

TL;DR

确保POST URL不以斜杠结尾。 - 但是Laravel 4.1用户首先检查下面的更新。

=======

当它工作时,它不是迷信 - 这是科学:-(

一旦我暴露了所有人的蠢货,我保证会自己找到答案,否则我将无法回答几个小时

我认为Laravel4的HTML消息本来可以提供更多信息。

grep按预期找到了重定向的来源:

grep -r "Redirecting to" *
vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RedirectResponse.php:        <title>Redirecting to %1$s</title>
vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RedirectResponse.php:        Redirecting to <a href="%1$s">%1$s</a>.

在那一点上,一个简单的回溯在Laravel4旋转中很早就找到了原点:

bootstrap/start.php:

...
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application;

$app->redirectIfTrailingSlash();

当我看到redirectIfTrailingSlash时,我意识到表单和jQuery发送的两个URL不一样:

... action="/search/?uid=1701">   <--- TRAILING SLASH AFTER 'search'

... url:   '/search',             <--- NO TRAILING SLASH
    data:  {
               uid  : 1701,
               query: $('#searchterm').val()
           },
...

为什么会发生这种情况我不太了解,但解决方案非常简单:

从POST操作字段中删除斜杠。

(并确保.htaccess没有规则将URL视为“目录”并添加斜杠 - 但如果有,则jQuery也会失败)。

UPDATE

显然,这个问题在Laravel 4.1中得到了审查。 upgrade提到

删除重定向尾随斜杠

在bootstrap / start.php文件中,删除对$ app-> redirectIfTrailingSlash()的调用。此方法现在不再需要,因为此功能现在由框架附带的.htaccess文件处理。

接下来,用这个处理尾部斜杠的新文件替换你的Apache .htaccess文件。

© www.soinside.com 2019 - 2024. All rights reserved.