我认为这段代码有很多错误。我需要的是创建一个表单,向控制器提交 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
}
}