Route post ajax Laravel

Asked

Viewed 284 times

0

Galera I have a search field by ajax in Aravel that works normally local but on the server it is returning error. When the search route (post -> http://site.com.br/cidadedigital/admin/prefeitura/search/) is triggered by the keyup event of error 301 Moved Permanently and then runs again (mysteriously) another route (get -> http://site.com.br/prefeitura/search/) which consequently returns error 404 not found because that second route really does not exist.

This error only occurs through ajax, when I give enter and the form is sent conventionally the route is found and the results are displayed correctly.

Has anyone ever had such problems? Can anyone tell me if this is a code problem or some server configuration?

That is the route that is in the Routes.php

Route::post('/prefeitura/search', 'Admin\PrefeituraController@search');

This is the . htaccess that is in the root folder of the Laravel (public folder files have also been moved to the root folder of the Laravel)

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

    RewriteEngine On

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

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

This is the ajax function that does the search and displays the found data

$("#searchPrefeitura").on('keyup',function(){

    $.ajax({
        url: 'http://site.com.br/cidadedigital/admin/prefeitura/search/',
        type: 'POST',
        dataType    : 'html',
        encode      : true,
        data        :{exp:$(this).val(), _token: $("#_tokenSearch").val()},
        beforeSend  : function(){
            $(".spinner").removeClass('hide');
        },
        success     : function(data){
            $("#listPrefeitura").html(data);
        },
        complete    : function(){       
            $(".spinner").addClass('hide');
            $("#limpabusca").fadeIn();
        },

        error   :   function(data)
        {
            //console.log(data.responseText);
        }
    });

});
  • Try to call in ajax only the url of the msm route as registered, not right? /city/search

  • In this case it gives only the Not Found error but it still didn’t work. The requested URL /prefecture/search was not found on this server.

  • But how can you give the 404 if the route is recorded like this... when you give the enter it works by going to the route? a /cidadedigital/admin/prefeitura/search ?

  • u put so msm? url: '/city/search',

  • Exactly like this. I tried with bar at the end too, and gave the same result. I also do not understand how it can be giving error if the route is correct.

  • is expensive, I don’t know msm... the url: '/cidadedigital/admin/prefeitura/search', tbm n worked?

  • if received a 302 would ask if it would be perhaps a question of route permission, but the 301...

  • Man thanks for the help. Now I got to settle here. I had to leave the url in ajax in this way /citypedigital/admin/city/search and in the route archive that way /city/search. I found it very strange behavior not to work with the bar (/) at the end of the routes. But that was the problem, no bar at the end works and with the bar does not work.

  • Mysteries but good that managed @user5103

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.