LSe*_*rni 4 php jquery redirect symfony laravel-4
我确信,另一个我没有睡觉的问题.我把它作为牺牲给老上帝墨菲发布:一旦我揭露了我的所有人看到的蠢事,我保证会自己找到答案,否则我将躲过几个小时(通过进一步的忏悔,然后我会发布答案).
我有一个HTML表单,呈现为
<form method="post" id="mysearch" action="/search/?uid=1701">
<input id="searchterm" type="text" name="query" />
</form>
Run Code Online (Sandbox Code Playgroud)
表单可以通过jQuery提交,$.POST其中包含"/ search"的url和数据,{ uid: '1701', query: $('#searchterm').val() } 并且可以正常运行.
如果我在输入内容后按ENTER键,从而覆盖jQuery提交,则会发生以下情况:
Route::post('/search', function() {...不会不被调用.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>
Run Code Online (Sandbox Code Playgroud)
这与此问题不同,因为预期会出现重定向,这是不希望出现的问题的答案.这是重定向本身,无缘无故地生成(现在).
我怀疑由于某种原因我触发了另一个答案中描述的"安全重定向",这不是由jQuery触发的(因为它将所有内容放在POST中,而在这里我在URL中有一个参数而另一个在POST,或者因为jQuery使用XHR).
我原本以为它可能是一个CSRF防御,但那个特定的路线没有被屏蔽.作为最后一个资源,我将CSRF保护路由并将令牌添加到表单中,即使它看起来有点像伏都教给我.在Rails中似乎发生了一些模糊相似的东西.
我没有一个,而不是两个,而是三个解决方案,巧妙地回避了为什么上面发生的问题:
keyUp表格中的事件.$('#search-button').click()...但我想完全没有按钮(我可以用jQuery做)并且完全没有jQuery.同时了解这里发生了什么.我99%肯定我错过了一些明显的东西.
我现在要去grep -r "Redirecting to" *整个框架源代码(我希望能找到一些内容Symfony/Components/HttpFoundation/ResponseRedirect)并从那里一步一步地进行.
LSe*_*rni 14
确保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>.
Run Code Online (Sandbox Code Playgroud)
在那一点上,一个简单的回溯在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();
Run Code Online (Sandbox Code Playgroud)
我一见到它redirectIfTrailingSlash,就意识到表单和jQuery发送的两个URL 不一样:
... action="/search/?uid=1701"> <--- TRAILING SLASH AFTER 'search'
... url: '/search', <--- NO TRAILING SLASH
data: {
uid : 1701,
query: $('#searchterm').val()
},
...
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况我不太了解,但解决方案非常简单:
从POST操作字段中删除斜杠.
(并确保.htaccess没有规则将URL视为"目录"并添加斜杠 - 但如果有,则jQuery也会失败).
显然,这个问题在Laravel 4.1中得到了回顾.该升级提到
删除重定向尾随斜杠
在bootstrap/start.php文件中,删除对$ app-> redirectIfTrailingSlash()的调用.不再需要此方法,因为此功能现在由框架中包含的.htaccess文件处理.
接下来,用这个处理尾部斜杠的新文件替换你的Apache .htaccess文件.
| 归档时间: |
|
| 查看次数: |
3054 次 |
| 最近记录: |