Your ajax looks fine, but the username, is it a variable or string ?
As mentioned by popolla, please post your routes and controller snippets.
Probably related to CSRF. Are you using that filter? Try to post the token as well -> csrf_token();
Hi there, I have the same problem I want to create a simple post in ajax but it seems I am having problem with the response. I always get a 500 Internal Server Error
<script>
$("document").ready(function(){
$("#frm").submit(function(e){
e.preventDefault();
var username = $("input#username").val();
var token = $("input[name=_token]").val();
var dataString = 'username='+username+'&token='+token;
$.ajax({
type: "POST",
url : "admin/login",
data : dataString,
success : function(data){
console.log(data);
}
},"json");
});
});//end of document ready function
</script>
<div id="loginfrm">
{{Form::open(array("","id"=>"frm"))}}
<!-- <form id="frm"></form> -->
<label id="username-label">Username</label>
<input type="text" name="username" id="username">
<label id="password-label">Password</label>
{{Form::password('password',array("class"=>"frmControl","id"=>"password"))}}
{{Form::submit('Create User')}}
</form>
</div>
Route::post('admin/login',array('before'=>'csrf','uses'=>function(){
$data = Input::all();
if(Request::ajax())
{
$u = new User;
$u->username = $data['username'];
$u->password = Hash::make(Input::get( $data['password']));
//if success
if($u->save()){
return 1;
}
//if not success
else{
return 0;
}
}
}));
My Problem is every time I submit I always get an error: POST http://localhost/hotelcraze/admin/login 500 (Internal Server Error)
I am new to Laravel 4. Please help me with this issue Thank you in advance :-)
I've got the same problem, did you ever find out what the issue was?
I think you forgot to put name="password" in your password.
you have to send method and token inside data```
$(document).on('click','#button',function(){
var dataId = $(this).data("id");
var token = $('meta[name="csrf-token"]').attr('content');
$.ajax({
type:'POST',
url:"{!! URL::to('url') !!}",
dataType: 'JSON',
data: {
"_method": 'POST',
"_token": token,
"id": dataId,
},
success:function(data){
console.log('success');
console.log(data);
},
error:function(){
},
});
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community