Support the ongoing development of Laravel.io →
posted 8 years ago
Requests
Last updated 1 year ago.
0

hola.... probaste con

return response('hola');

this is how I do it

Form

<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);
}
Last updated 8 years ago.
0

Still does not work and do not understand. Everywhere says it works well.

0

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>
Last updated 8 years ago.
0

I included in my code jquery

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min....;></script>
0

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.

0

you need to enable CORS! or change the form action URL

#from 
action="http://vivianaglonski.web/";

# login to 

action="{{ url('/login') }}"
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nikoxd123 nikoxd123 Joined 12 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.