Search code examples
ajaxlaravelapache

Laravel. AJAX does not work on apache


When I run this code by php artisan server it looks good but when I want use apache there is 404 response. But from beginning. Below are the fragments of the code:

Ajax:

        $('.item_traveler').select2({
          placeholder: 'Wpisz nazwisko podróżnego',
          language: "pt-PL",
          "language": {
              "noResults": function(){
                  return "Nie znaleziono podróżnego";
              },
              "searching": function(){
                  return "Szukam w bazie podróżnych...";
              }
          },
          ajax: {
              url: '/autocomplete-ajax-traveler',
              dataType: 'json',
              delay: 250,
              processResults: function (data) {
                  return {
                      results:  $.map(data, function (item) {
                          return {
                              text: item.lastname+" "+ item.firstname+" | "+item.email+" | "+ item.city+", ul."+ item.address_street,
                              id: item.id
                          }
                      })
                  };
              },
              cache: true
          }
    });

Routing:

Route::get('/autocomplete-ajax-traveler', 'AutocompleteController@dataAjax_traveler');

Controller:

    public function dataAjax_traveler(Request $request)
{
    $data = [];
    if($request->has('q')){
        $search = $request->q;
        $data = DB::table("travelers")
            ->select("id","lastname","firstname","email","city","address_street")
            ->where('lastname','LIKE',"%$search%")
            ->get();
    }
    return response()->json($data);
}

When I use php artisan serve it looks like: php artisan serve version

When I use apache it looks like: enter image description here

And my .htaccess file contains:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I'm working on it for 5 days without a result. Please help


Solution

  • on apache, You have to give full path. ie.

    url: localhost/{Project folder name}/public/autocomplete-ajax-traveler

    This way the request will reach your application.