Send and collect data sent with AJAX

1

I am doing a small system but I can not send the data with serialize() to my controller. How can I solve it?

JavaScript that sends the data via AJAX:

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

$(document).on("submit", ".form_entrada",
  function(e) {

    e.preventDefault();

    $('html, body').animate({
      scrollTop: '0px'
    }, 200);

    var formu = $(this);
    var quien = $(this).attr("id");

    if (quien == "formEditUser") {
      var
        varurl = "updateUser";
      var
        divresul = "notificacion_resul_fanu";
    }

    $.ajax({
      type: 'POST',
      url: varurl,
      datatype: 'json',
      data: formu.serialize(),
      success: function(result) {
        console.log(result);
      }
    });
  });

Controller:

public function updateUser(){
    $input = request()->all();
    return $input;
}

The PHP file with the form:

<form id="formEditUser" method="post" action="updateUser" class="form_entrada">

  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="hidden" name="id" value="<?= $usuario->id; ?>">
  <div class="box-body">
    <div class="form-group">
      <label for="correo">Email</label>
      <input type="text" class="form-control" id="correo" name="correo" placeholder="[email protected]" value="<?= $usuario->email; ?>">
    </div>
    <div class="form-group">
      <label for="dni">Nombre</label>
      <input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre..." value="<?= $usuario->name; ?>">
    </div>
    <div class="form-group">
      <label for="nombre">Tipo de Usuario</label>
      <input type="text" class="form-control" id="T1ID" name="T1ID" placeholder="Tipo..." value="<?= $usuario->T1ID; ?>">
    </div>
    <div class="form-group">
      <label for="apellido">Habilitado</label>
      <input type="text" class="form-control" id="status" name="status" placeholder=" 0 ó 1" value="<?= $usuario->status; ?>">
    </div>
    <div class="form-group">
      <label for="celular">Celular</label>
      <input type="text" class="form-control" id="cellphone" name="cellphone" placeholder="999-999-999" value="<?= $usuario->cellphone; ?>">
    </div>
    <div class="form-group">
      <a href="users.index" class="btn btn-sm btn-default">Cancelar</a>
      <!--<a href="{{ url('persons.index') }}" class="btn btn-default">Cancelar</a>-->
      <button id="btnsubmit" ; type="submit" class="btn btn-sm btn-primary pull-right">Registrar</button>
      <!--<a href="#" class="btn btn-sm btn-primary pull-right">Registrar</a>-->
    </div>
</form>

web.php

Route::get('/', function () {
    return view('adminlte::auth.login');
});

Route::group(['middleware' => 'auth'], function () {

    Route::get('users','UsuarioController@getList');
    Route::get('users.index','UsuarioController@index')->name('users.index');

    Route::get('formNewUser','UsuarioController@formNewUser');
    Route::get('formEditUser/{id}','UsuarioController@formEditUser');
    Route::post('updateUser', 'UsuarioController@updateUser');

    //este ultimo es el que apunta ami controlador
});

The error is as follows:

  

POST link 419 (unknown status)? d41d: 9536

    
asked by John Rivera Bernuy 07.03.2018 в 21:13
source

1 answer

0

I just read your last comment, it is likely that your ajax call needs to add to the header the csrf_token, you can do it in the following way:

1: In the head of your HTML you put the following goal if you do not have it added:

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

2: Then add it to the header of the AJAX call like this:

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

$.ajax({
    type: "POST",
    url: "turuta",
    data: $("#form").serialize(),
    dataType: "json",
    success: function(datos) {
        console.log(datos);
    },
    error: function (request, status, error) {
        //hacer algo
    }
});

I hope you serve, greetings!

    
answered by 07.03.2018 в 21:23