Rebelutionairy's avatar

404 Not Found. Ajax Route get request not working as i wanted to :-(

Been busy with this since yesterday and believe me when i say i tried whatever i could think of and find on the net. but nothing helped. so im giving up on this one till a hero appears.

Route::post ('/search', [SearchController::class, 'send_http_request'])->name('searchReq');

      var routeSearch = "{{ route('searchReq') }}"

      ajaxService(routeSearch, value, label);

 $.ajax
       type:'GET',
       url:routeSearch,
       dataType: 'json', 
       encode  : true,
       data:{value:value, label:label},

0 likes
11 replies
jlrdw's avatar

Does it work if you use a full url, I usually use full and have had no problems with ajax.

1 like
Rebelutionairy's avatar

ill give it a try. that has been the only thing i didnt try i think.

Post: i tried the following:

Route::post('/search', [SearchController::class, 'send_http_request']);
var routeSearch = "/search"

That didnt solved it!

Could it be something live server access permissions (htaccess or so..)

automica's avatar

@rebelutionairy the route you've supplied is a POST,

Route::post('/search', [SearchController::class, 'send_http_request']);

but in your Ajax request, you're trying to get it with a GET.

 $.ajax
       type:'GET',
       url:routeSearch,
       dataType: 'json', 
       encode  : true,
       data:{value:value, label:label},

maybe using the same verb for both would solve your issue?

 $.ajax
       type:'POST',
       url:routeSearch,
       dataType: 'json', 
       encode  : true,
       data:{value:value, label:label},
1 like
Rebelutionairy's avatar

I also tried playing with that before. didnt work out for me. Even now they both set to GET.

Any other ideas for me :-D? This error will make me quit my career so soon.

automica's avatar

you'll need to first ensure your Laravel route is responding. if you are using a form on your search page, your original code looks like it would work best using POST verb.

I would suggest downloading 'Postman' and setting up a request to query your endpoint that way. This will confirm your endpoint is responding.

I would also suggest you restructure your Ajax request to add means to log response.

$.ajax(
   {
      type: 'post',
      url: routeSearch,
      data:{
		 "value":"value", 
		 "label": "label"
		},
      success: function (response) {
        alert("Success !!");
      },
      error: function () {
        alert("Error !!");
      }
   }
);

you should be able to use network tab in your dev tools to see if you are hitting the correct url. If you're php endpoint isnt working though, the Ajax request won't work either.

1 like
jlrdw's avatar

Show your whole js code for that. Also I don't think you need encode, try without.

2 likes
Rebelutionairy's avatar

I really wanted to pass all the code in here but first day and THEGAME sees my code blocks as links somehow. The encode thing was just a desperate action :-D.

jlrdw's avatar

Post code here, not a link. Just the entire Ajax code and the controller code where you receive it.

Also fix your route, try it named correctly.

Rebelutionairy's avatar

Even with postman im receiving the response 404 NOT FOUND. without any further details.. Could it be a server permission thingy?? im clueless.

Route::get('/search', [SearchController::class, 'send_http_request']);
const ajaxService = function(routeSearch, value, label){
    
    $.ajax({
      type:'GET',
      url:routeSearch,
      headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
      data:{value:value, label:label},
      success:function(data){
         alert(data.success);
      },
      error: function(response) {
          console.log(response);
      }
   });
}

  const ac = new Autocomplete(document.getElementById('autoCompletionInput'), {
    data: [{label: "I'm a label", value: 42}],
    maximumItems: 10,
    treshold: 1,
    highlightTyped: true,
    highlightClass: 'text-primary',
    onSelectItem: ({label, value}) => {
      var routeSearch = "/search"

      ajaxService(routeSearch, value, label);
    } 
});

@jlrdw , @automica: Finally, i could add the code. i would love to have your expertise vision on this.

Rebelutionairy's avatar

The issue was the route cache. i only had to register them. hadn't noticed that i had to route:cache in artisan... im closing this question.

solut: php artisan route:cache. everytime when you change something to your route.

automica's avatar
automica
Best Answer
Level 54

@rebelutionairy You shouldn’t need to run this command in development.

I suspect your problem is that you actually need to clear your cache for your new routes to show.

By running route:cache you are regenerating the cache. If you destroy the cache with php artisan route:clear you will always have access to new routes if you’d change your routes file and don’t need to run any route commands.

Caching your routes is only necessary on production.

1 like

Please or to participate in this conversation.