Help to resolve GET 500 error (Internal Server Error)

0

I am trying to create a dynamic select with ajax, when selecting a role, users with the selected role should be displayed.

The script that I am using is the following

<script type="text/javascript">
    $(document).ready(function(){
        $('#sucursal').on('change', function(e){
            console.log(e);

            var id = e.target.value;

            $.ajax({
                type: 'get',
                url: 'ajax'+id,
                scuccess: function(data){
                    console.log(data);
                }
            });
        });
    });
</script>

My route is as follows

Route::get('ajax/{id}', ['as'=>'ajax','uses'=>'RecepcionController@ajax']);

And in my controller I have the following

public function ajax($id) {
  $id    = $id;
  $nombre = User::where('role_id','=',$id)->get();
  return response()->json($nombre);
 }
    
asked by José 09.05.2018 в 20:10
source

3 answers

1

You should try to put a / ajax

$.ajax({
      type: 'get',
      url: 'ajax/'+ id,
      scuccess: function(data){
      console.log(data);
    }
});
    
answered by 09.05.2018 в 20:14
0

Usually that error occurs when something bad happened on your server. You should go to see what the console and the network are responding to, and if the answer is blank, activate the trace of errors in the PHP. It lacks a slash to your route, for how your controller behaves

url: 'ajax/'+id,
  

The 500 Internal Server Error is a very general HTTP status code that means something has gone wrong on the server's website, but the server could not be more specific on what the exact problem is.

error_reporting(-1);

or

ini_set('error_reporting', E_ALL);

Enable PHP errors

It may be different since you are using laravel

    
answered by 09.05.2018 в 20:18
0

I think your problem is the same as in this case Select dependents with ajax and laravel for an edit view .

You are making the request to a relative route and you should do it to an absolute one.

app_url: 'http://localhost:8000/';

$.ajax({
    type: 'get',
    url: app_url + 'ajax/' + id,
    scuccess: function(data){
        console.log(data);
    }
});
    
answered by 10.05.2018 в 13:15