send form with ajax in laravel

2

I have a problem with the ajax that I am using, I need to send those variables but I get an error in the console, I can not find the route and I need to know how to send it by laravel obviously

$.ajax({
        type: 'POST',
        url: '{{url("actualizar_baja")}}',
        data: {calibre:calibre, medio:medio, NCF:NCF, cantCD:cantCD, rango:rango, proteccion:proteccion, id:id, num:num},

        //funcion para retornar los resultados y mostrarlo en cada td
        success: function(resultado) {
             $('#tabla_ntc_2050_utilizar'+id).html(resultado.split("#")[1]);
        },
});

Console error

  

jquery-3.3.1.min.js: 2 POST   design.test / Memories / 2 /% 7B% 7Burl ()% 7D% 7D / update_down 404 (Not   Found)

    
asked by Daniel Garcia 06.07.2018 в 17:54
source

1 answer

1

You can not use the blade inside javascript , but you can choose this solution, you are not sending the CSRF Token in your post .

First use a span in which you store your route as text:

<span id="url_actualizarbaja" hidden="">
     {{url("actualizar_baja")}}
</span>

Consider also saving the csrf token in a view tag

<meta name="csrf-token" content="{{ csrf_token() }}" />

Then call that route from the text of span , also send the token csrf

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
        type: 'POST',
        url: $("#url_actualizarbaja").text(),
        data: {
          _token: CSRF_TOKEN, //aquí considera el token csrf
          calibre:calibre, 
          medio:medio, 
          NCF:NCF, 
          cantCD:cantCD, 
          rango:rango, 
          proteccion:proteccion, 
          id:id, 
          num:num
        },

        //funcion para retornar los resultados y mostrarlo en cada td
        success: function(resultado) {
             $('#tabla_ntc_2050_utilizar'+id).html(resultado.split("#")[1]);
        },
});
    
answered by 06.07.2018 / 19:00
source