2

我认为这段代码有很多错误。我需要的是创建一个表单,向控制器提交 XHR,获取数据,发送到模型,检查验证,返回控制器输出验证,然后发送到视图消息“错误或成功”

路由.php

Route::get('checkValidationEmail', 'HomeController@checkValidationEmail');

<?php
echo Form::open(array('url' => 'checkValidationEmail', 'class' => 'form_notify'));
echo Form::text('Email', Input::old('Email'),  array('placeholder'=>'Email', 'class' => 'hg'));
echo Form::close() 
?>

<a href="#" class="send_email"> Notify Me!</a>

好的,这工作正常,接下来,问题开始了。


AJAX

<script>
    $(function () {
        $(".send_email").click(function () {
            email = $('.hg').val();

            $.ajax({
                type: "POST",
                url: 'checkValidationEmail', //what is the correct url?
                data: {
                    email: email
                }
            }).done(function (msg) {
                alert(msg);
            });

        });
    });
</script>

500 错误:

{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException","message":"","file":"C:\\VertrigoServ\\www\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php","line":1439}}

控制器

class HomeController extends BaseController {
       public function checkValidationEmail() {
           //how to get the data from form and pass to the model, more exactly validate($input)

                   if ($validation) {
                      return true;  
                   }
                   else {
                      return 'incorrect email'; //how send this message to the view?
                   }
       }
}

模型

class Home extends Eloquent {

    protected $table = 'emails';

    public function validate($input) {
       //validations
    }

}
4

1 回答 1

5

在您的路线中,您被定义checkValidationEmailGET请求,并且从您的 ajax 中,您尝试将其作为POST请求访问。所以这会抛出MethodNotAllowedHttpException. 所以你必须改变你的路线,

Route::get('checkValidationEmail', 'HomeController@checkValidationEmail');

至,

Route::post('checkValidationEmail', 'HomeController@checkValidationEmail');

或者,

Route::any('checkValidationEmail', 'HomeController@checkValidationEmail');
于 2013-10-02T07:55:17.703 回答