Reputation: 1180
I have the error MethodNotAllowedHttpException
when I submit form data using ajax
.
HTML
<form class="form-signin" id="loginForm" role="form" method="POST">
// Form
</form>
<script>
$('#loginForm').submit(function () {
initLogin($('#email').val(),$('#password').val());
});
</script>
JavaScript
function initLogin(email, password) {
$.ajax( {
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url:'loginModal',
method:'POST',
data: {
strEmail: email,
strPassword: password
},
success: function( bolUpdated ) {
alert('yes');
},
fail: function() {
alert('no');
}
});
}
Route
Route::post( 'loginModal', 'Auth\LoginController@loginModal' );
Controller
public function loginModal( Request $request ) {
Log::info('test');
}
I tried changing the form's type but no luck. Any idea what might be the issue?
Upvotes: 0
Views: 70
Reputation: 97672
You have to stop the form from submitting if you're going t use ajax
$('#loginForm').submit(function (e) {
e.preventDefault(); //<-- here
initLogin($('#email').val(),$('#password').val());
return false; //<---- or here
});
otherwise the form will make a post request to the current page(since action is not there) which has no route for a post request.
Upvotes: 1
Reputation: 7489
prevent default event of form because you have not defined any action for form and its request is being sent to current page
$('#loginForm').submit(function (event) {
event.preventDefault();
initLogin($('#email').val(),$('#password').val());
});
Upvotes: 1