I'm having an issue with my ajax
form submission in my Laravel 5
project. I am getting the following error in my browser console:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I've tried many suggestions found in other posts and sites with no luck. Here is my Ajax:
$.ajax({'url': './subscribe'
,'data' : {
"_token": $('meta[name="csrf-token"]').attr('content')
,"email" : $('#subscribe-email-input').val()
}
,'method': 'POST'
,'success': function(data){
console.log( data );
}
,'error': function(data){
console.log( 'oops', data );
}
});
I have the following route defined in my routes/web.php
file:
Route::post('subscribe', ['uses' => 'SubscribeController@validate', 'as' => 'subscribe.route']);
And the controller is pretty simple:
class SubscribeController extends Controller
{
/*
* Get email address from DB if exists
*
* @param string email, email address to search and return data from DB
*
* @returns $email
*/
public function validate ( Request $request )
{
$email = addslashes($request->email) ;
if ( $email )
{
$email = \App\Subscribe::where('email', '=', $email)->get();
if ( $email->isEmpty() )
{
self::executeInsertQuery ( $email );
return json_encode( array('body' => 'You have subscribed!') );
} else {
return json_encode( array('body' => 'You are already subscribed!') );
}
}
}
}
I would post the code within self::executeInsertQuery(...)
but I don't think its relevant considering the subscribe
route doesn't even seem to be getting to the validate
method.
Any suggestions or advice on how to resolve the issue would be greatly appreciated!! Thank you in advance!
The problem is the validate
function is a predefined method in the Laravel controllers so you override it and that causes problems. try to change it to something else.