jose angarita
jose angarita

Reputation: 199

Ajax with Jquery in Laravel 5.0

people i new to laravel and i have a problem...

I have a question I'm trying to use jquery to find information in the database show them:

I have my form with the name "create.blade.php"

@extends('admin.usersprofile')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div class="panel panel-default">
                    <div class="panel-heading">Nuevo Perfil de Usuario </div>

                    <div class="panel-body">
                        {!! Form::open(['route'=>'admin.profile.store','method'=>'POST','class'=>'']) !!}

                            <div class="form-group">
                                {!! Form::label('email','Correo electronico') !!}
                            </div>
                            <div class="form-group">
                                {!! Form::text('email',null,['class'=>'form-consult','placeholder'=>'Por favor ingrese su email']) !!}
                                <input type="button" class="btn btn-primary" name="consultar" id="consultar" value="Consultar">
                            </div>
                        <div class="form-group">
                            {!! Form::label('first_name','Genero') !!}
                            {!! Form::select('gender',[''=>'Seleccion un genero','M'=>'Masculino','F'=>'Femenino'],null,['class'=>'form-control']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('gender','Genero') !!}
                            {!! Form::select('gender',[''=>'Seleccion un genero','M'=>'Masculino','F'=>'Femenino'],null,['class'=>'form-control']) !!}
                        </div>
                            <div class="form-group">
                                {!! Form::label('twitter','Twitter') !!}
                                {!! Form::text('twitter',null,['class'=>'form-control','placeholder'=>'Por favor ingrese tu usuario twitter']) !!}
                            </div>
                            <div class="form-group">
                               {!! Form::label('website','Pagina Web o Blog') !!}
                               {!! Form::text('website',null,['class'=>'form-control','placeholder'=>'Por favor Ingresa la URL de tu sitio web']) !!}
                            </div>
                        <div class="form-group">
                            {!! Form::label('birthdate','Fecha de nacimiento') !!}
                            {!! Form::text('birthdate',null,['class'=>'form-control','placeholder'=>'Por favor Ingresa tu fecha de nacimiento']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('bio','Sobre mi') !!}
                            {!! Form::textarea('birthdate',null,['class'=>'form-control','placeholder'=>'Describe aqui algo sobre ti']) !!}
                        </div>
                            <button type="submit" class="btn btn-default">Guardar Perfil</button>
                        {!! Form::close() !!}

                    </div>

                </div>
            </div>
        </div>
    </div>
 @endsection

I have my route called "Profile" if you can see this in a directory called Admin:

Route::group(['prefix' => 'admin', 'namespace' => '\Admin'],function(){

    Route::resource('profile','UsersProfileController');
    Route::post('test','UsersProfileController@consultar');

 });

Here the driver is called "UsersProfile Controllers":

 public function consultar()
    {

        if(Request::ajax())
        {
            $json['nombre']="";
            $json['twitter']="";
            $correo=Input::get('email');
            $result= User::orderBy('first_name', 'ASC')
                ->leftJoin('user_profiles', 'users.id', '=', 'user_profiles.user_id')
                ->where('email', '=', $correo)
                ->get();
            foreach ($result as $res)
            {
                $json['nombre']=$res->full_name;
                $json['twitter']=$res->twitter;

            }
            return json_encode($json);

        }


    }

Finally I have my Jquery:

 $(document).ready(function(){

    $("#consultar").click(function(){
        var correo=$("#email").val();
        var llave=$('input[name=_token]').val();

        $.ajax
        ({
            url:'test',
            type:'post',
            dataType:'json',
            data:{'email':correo,'_token': llave},


            success: function (data)
            {
                var nombre=data.nombre;
                var twitter=data.twitter;

                alert(nombre);
                alert(twitter);
            }


        });
    });
 });

The issue is that when I run this is what brings me back:

MethodNotAllowedHttpException in RouteCollection.php at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE'))

Now if I check my routes you can see that the test route exists:

|   | POST | admin/test | Course\Http\Controllers\Admin\UsersProfileController@consultar |

If they realize everything is perfect but do not understand why it does not work and gives me this message MethodNotAllowedHttpException

I hope I can show the right path, and I do not see ... thanks.

Upvotes: 2

Views: 153

Answers (2)

jose angarita
jose angarita

Reputation: 199

the problem was solved for my...

The problem was that the route in the AJAX Always thought:

url: 'test'

but the path in the browser Always thought:

http://127.0.0.1/Course%201.1/public/admin/profile/create

"admin/profile/create" 

I needed to get out of the "profile" directory so in my AJAX:

url:'../test'

That was all

Upvotes: 0

swatkins
swatkins

Reputation: 13640

It looks like your route is admin/test, but your AJAX request just goes to test

Upvotes: 1

Related Questions