我正在尝试发布用户电子邮件订阅我的网站,但我的控制台一直给我一个404例外的路由。我有一个名为beta的表,以及App \ Beta.php中名为Beta的模型。这是我的路线:
use App\Beta;
use Illuminate\Http\Request;
Route::post('/email',['middleware'=>'csrf',function (Request $request){
$validator=Validator::make($request->all(),[
'email' => 'required|max:64|email|unique:betas,email'
]);
//if validator passes save the email in the database
if($validator->passes()){
$beta = new App\Beta;
$beta->email=$request->email;
$beta->save();
$response=array(
'status' => 'Email posted successfully'
);
}
else{
$messages = $validator->messages();
$response=array(
'messages' => $messages->first('email'),
'status' => 'Email post failed',
);
}
return response()->json($response);
}]);
以下是我的js文件:
$("#betaForm").submit(function(){
$(".loadingPic").show();
var email=$("#emailInput").val();
console.log("email:"+email);
//pause for a bit to see pretty loading icon
console.log("before setTimeout");
var load=setTimeout(function(){return undefined;}, 1000);
console.log("after setTimeout");
if(email==""){
$("#warningMessage").html("please input your email");
$("#warningMessage").show();
}
else{
$.post({
url: $(this).prop('action'),
data: {
"_token": $( this ).find( 'input[name=_token]' ).val(),
"email": email
},
success: function(result){
if (result.status!='Email posted successfully'){
$("#warningMessage").html(result.messages);
$("#warningMessage").removeClass( "alert-success");
$("#warningMessage").addClass("alert-danger");
$("#warningMessage").show();
}
else{
$("#betaForm button").prop("disabled",true);
$("#betaForm button").html("Awesome! Well Be In Touch.");
}
},
dataType: 'json'
});
}
$(".loadingPic").hide();
return false;
});
按下提交按钮后,我在浏览器控制台中收到错误。
答案 0 :(得分:2)
我认为'中间件'上的问题=>'csrf'。