hola.... probaste con
return response('hola');
this is how I do it
<form class="form-horizontal ajax-form" role="form" method="POST" action="{{ url('/api/v1/user') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="email" class="form-control" name="email" value="">
<input type="password" class="form-control" name="password">
<span class="showError"></span>
<button type="submit" class="btn btn-primary btnSubmit">Save</button>
</form>
#script
<script>
$(".ajax-form").submit(function(e)
{
e.preventDefault();
$('.btnSubmit').attr('disabled','disabled');//disabled submit button
var form = $(this);
var url = form.attr('action');
var method = form.find('input[name="_method"]').val() || 'POST' ;
var data= form.serialize();
$.ajax({
url : url,
type: method,
data: data,
dataType: 'json',
success: function( json )
{
$('.ajax-form')[0].reset(); //clear form
$('.btnSubmit').removeAttr('disabled'); //enable button
},
error : function ( jqXhr, json, errorThrown )
{
var errors = jqXhr.responseJSON;//get Larevel errors
if( jqXhr.status == 422) // validation failed
{
var errorsHtml ='';
$.each( errors, function( key, value ) {
errorsHtml += '<li>' + value[0] + '</li>';
});
$('.showError').html( errorsHtml );
}
$('.btnSubmit').removeAttr('disabled');
}
});
});
</script>
# Controller
public function store(Request $request)
{
$v = Validator::make( $request->all() , [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6',
]);
if( $v->fails() )
{
return response( $v->errors() ,422);
}
$user = new User;
$user->email = $request->email;
$user->password = bcrypt( $request->password );
$user->save();
return response( [ 'success'=>true, 'data' => $user ] , 200);
}
Still does not work and do not understand. Everywhere says it works well.
did you insert jquery?
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
I changed your form a bit and it works
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<form class="ajax-form" method="POST" action="{{ url('/login') }}">
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
<div class="form-group">
<input type="text" placeholder="Usuario" name="user" id="email" class="form-control">
</div>
<div class="form-group">
<input type="password" placeholder="Contraseña" name="password" id="pass" class="form-control">
</div>
<button type="submit" class="" id="login">Save</button>
</form>
<script>
$(".ajax-form").submit(function(e)
{
e.preventDefault();
var form = $(this);
var url = form.attr('action');
var datos = form.serialize();
var logica = Logica( url, datos );
});
function Logica(url, datos )
{
$.ajax({
type : "POST",
url : url,
data : datos,
dataType: 'json',
success : function( json )
{
alert( json );
}
});
}
</script>
I included in my code jquery
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
After installing google chrome, gave me an error that was understood, the script is good, the error it gives is the following
XMLHttpRequest can not load http: //vivianaglonski.web/login. No 'Access-Control-Allow-Origin "header is present on the requested resource. Origin 'http: //vivianaglonski.prog' THEREFORE is not allowed access.
you need to enable CORS! or change the form action URL
#from
action="http://vivianaglonski.web/"
# login to
action="{{ url('/login') }}"
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community