简体   繁体   English

表单提交在Laravel中不起作用

[英]Form submission wont work in Laravel

I used POST method in my form to submit the value. 我在表单中使用了POST方法来提交值。 Here is my form code: 这是我的表单代码:

<form action="{{ url('/') }}" method="POST">
    <input type="hidden" name="value1" value="one" />
    <input type="hidden" name="value2" value="two" />

    <input type="submit" value="SEND!" />
</form>

and router.php code: 和router.php代码:

Route::get('/', function()
{
    return View::make('index');
});

Route::post('/', function()
{
        $data = Input::all();
        var_dump($data);
});

whether every time I pressed SEND button it shows the index file. 是否每次按SEND按钮都会显示索引文件。 When I tried commenting out the get method. 当我尝试注释掉get方法时。 Now it shows a MethodNotAllowedHttpException error. 现在,它显示了MethodNotAllowedHttpException错误。 On error message it shows that request method is GET 错误消息显示请求方法为GET

在此处输入图片说明

What should I do now? 我现在应该怎么办? Is that a bug? 那是个错误吗? Or something wrong in my script? 还是我的脚本有问题?

See https://github.com/laravel/framework/issues/1804 . 参见https://github.com/laravel/framework/issues/1804

What I have seen is that Laravel removes all trailing '/' from the URL by redirecting to the url without '/' with code 301. Your browser will be follow this redirection with a GET request instead of a new POST request. 我所看到的是Laravel通过使用代码301重定向到不带'/'的URL来删除URL中所有尾随的'/'。您的浏览器将通过GET请求而不是新的POST请求来遵循此重定向。

I had the same issue when posting to any url with a trailing '/'. 发布到任何带有尾随“ /”的网址时,我遇到了同样的问题。 Why don't you process the post at another url like '/post' to see if this is really the issue? 您为什么不以“ / post”之类的其他网址处理帖子,以查看这是否真的是问题所在?

Which version of Laravel are you using? 您正在使用哪个版本的Laravel? Laravel v4 comes with CSRF(Cross site request forgery) protection baked in. You need to include a hidden _token field by getting Laravel to generate one for you use Form::token() if you use Form::open() method instead it will add the token field for you. Laravel v4附带了CSRF(跨站请求伪造)保护。如果需要使用Form::token()则需要让Laravel为您生成一个隐藏的_token字段Form::token()如果您使用Form::open()方法来代替它)将为您添加令牌字段。 Laravel will automatically look for that _token field and if it isn't found, or is not a correct token it will throw an exception. Laravel将自动查找该_token字段,如果找不到该字段,或者它不是正确的令牌,它将抛出异常。

Edit: Check out Filters.php in your app/ directory for more info on the CSRF filter. 编辑:在您的app /目录中查看Filters.php以获取有关CSRF过滤器的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM