简体   繁体   中英

Laravel json return 404

I try to get list of users addresses on dropdown but i get 404 error on network also when i test it by url it returns 404 Page Not Found

Logic

  1. User can have many address
  2. Each address has user_id column in addresses table (I can get them from address table by their user_id)

What I need?

1- Select user in dropdown

2- Show his/her addresses in other dropdown

My controller:

public function getAddressList($user_id)
{
  $address = Address::where('user_id',$user_id)->get();
  return response()->json($address);
}

Route:

Route::get('/getAddressList/{user_id}','OrderController@getAddressList');

Blade:

// Select boxes
<div class="row bg-danger mb-20" style="padding: 7px;">
                      <div class="col-md-6">
                        {{ Form::label('user_id', 'Buyer') }}
                        <select name="user_id" class="form-control">
                            <option class="form-control" value="">Select user</option>
                             @foreach($users as $user)
                              <option value="{{ $user->id }}">{{ $user->name }}</option>
                            @endforeach
                        </select>
                      </div>

                      <div class="col-md-6">
                        {{ Form::label('address_id', 'Buyer Address') }}
                        <select name="address_id" class="form-control">
                            <option class="form-control" value="">Select Address</option>
                        </select>
                      </div>
                  </div>


//Javascript codes
<script type="text/javascript">
$(document).ready(function() {
  $('select[name="user_id"]').on('change', function() {
    var userID = $(this).val();
    if(userID) {
      $.ajax({
          url: '{{ url('getAddressList') }}/'+encodeURI(userID),
          type: "GET",
          dataType: "json",
          success:function(data) {
          $('select[name="address_id"]').empty();
          $.each(data, function(key, value) {
              $('select[name="address_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['address'] +'</option>');
              });
          },
      });
    }else{
      $('select[name="address_id"]').empty();
    }
  });
});
</script>


PS: if i visit: `http://domain.dev/getAddressList/1` i get 404 Page not fount as well as inspect network tab

Your route definition

Route::get('/getAddressList/{user_id}','OrderController@getAddressList');

must have been placed under group definition with some prefix .

Rectifying the route definition placement would resolve your issue.

With respect to the comments, you could revert back your controller method signature to

public function getAddressList($user_id) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM