Laravel and Ajax - Doubt about route

Asked

Viewed 118 times

0

Using the Framework Laravel 5.5, How do I call the following route in Ajax?

Route:

Route::group(['prefix' =>'paineladmin', 'namespace' =>'PainelAdmin', ], function(){

Route::post('galeriaArquivos', 'GaleriaimgController@arquivos')->name('galeriaArquivos');

});

Ajax code

function buscar($tamanho){
    // repassando as variaveis do php  
    var pasta = $('#pasta').val();  
    var tamanho = $tamanho;  


    // utilizando o split para quebrar o diretorio e receber somente o nome da pasta
    var dirimg = pasta.split("/galeriaimg/");

    var caminho = '/paineladmin/galeriaimg/arquivos';
    var diri =  '/images/'+tamanho+'/galeriaimg/';

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    var data = {
      tamanho: tamanho,
      diretorio: dirimg[1], 
    }

    $.ajax({
        type: 'POST',
        dataType: 'json',  
        url: caminho,      
            data:data,
            success: function(result){ 

               $.each(result, function(key, value){
                    var container = '<div class="col-md-4" id="col-'+key+'">';
                    container +=        '<div class="img-wrap">';
                    container +=            '<img src="'+diri+dirimg[1]+'/'+value+'" class="img-return" alt="galeria" "/>';    
                    container +=            '<a href="#" class="btn btn-default btn-sm delete" onclick="excluir('+key+')"><i class="fa fa-trash"></i></a>'; 
                    container +=         '</div>';    

                    container +=         '<input type="hidden" name="imagemgaleria['+key+'][endereco]" value="/galeriaimg/'+dirimg[1]+'/'+value+'" />';    
                    container +=         '<input type="text" placeholder="Título Imagem" name="imagemgaleria['+key+'][tituloimagem]" class="form-control required inputgaleria" />';   
                    container +=         '<textarea name="imagemgaleria['+key+'][descricao]" placeholder="Descricao" class="form-control inputgaleria" ></textarea>';   
                    container +=    '</div>';   

                    $('#galeriaimg').append(container);
               });

               /* monstrando os botoes que foram ocultados.*/
               $('.oculto').show();
              $('.group').remove();
         }
    });
}

I’ve tried calling {{ route('galeriaArquivos') }} or by following the data from those posts link Stackoverflow

  • Give a php Artisan route:list and take the correct route.

  • @marcosXavier thanks! I did what you went through and I got it solved! It was like this in the file ajax var path = 'galeriaArchives'; hugs,

  • Perfect. I could go into more detail about how you solved it, maybe help someone. I believe that using something like path = {{route(model.galeriaArchives)}} would also work. You would need to see the output of the command I suggested.

1 answer

-1


The question is so basic that it’s hard to imagine that this is it: To URL correct to be called is:

http://localhost/PASTA_DO_PROJETO/PASTA_ONDE_INSTALOU_O_LARAVEL/public/paineladmin/galeriaArquivos

Then return what you want, I took the test like this:

Route::group(['prefix' =>'paineladmin', 'namespace' =>'PainelAdmin', ], function(){
    Route::get('galeriaArquivos', function () {
        return response()->json(['message' => 'FUNCIONA',  API Rest', 'status' => 'Conectado']);;
    })->name('galeriaArquivosa');
});
  • hello! the way you went does not work, but I managed to return putting the way in the Ajax file, var path = 'galeriaArchives';

  • How strange, I took the test in my project before sending you, here I could call good as I told you, I’m using Laravel 5.4

  • quiet! the important thing is that I managed to solve! = ] here I am using the Standard 5.5, thank you! hugs

Browser other questions tagged

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