Ad

Laravel 5 Route Not Found 404

- 1 answer

I am trying to post user emails to subscribe to my site but my console keeps giving me a 404 exception for the route. I have a table called betas, and a model called Beta in App\Beta.php. Here is my route:

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);
}]);

Below is my js file:

$("#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;
    });

When the submit button is pressed, I receive the error in my browser console.

Ad

Answer

I think issue on 'middleware'=>'csrf'.

Ad
source: stackoverflow.com
Ad