Get form data by ajax - POST method worked - PUT method not

0

I have a form that sends the information to the controller by ajax.

When adding a record using type: POST if it works since doing dd ($ request-> all ()) shows me the array with the data.

function saveReg() {
    var datos = new FormData();
    datos.append('Date', $('#Date').val());
    datos.append('file', $('input[type=file]')[0].files[0]);
    $.ajax({
        type: 'POST',
        url: '{{url('/emp/newReg')}}',
        data: datos,
        contentType: false,
        processData: false,
        success: function (data) {
            // location.reload();
        },
        error: function (data) {
            console.log(data);
            showAjaxErrors(data, 'error_msg');
            markError(['Date', 'file']);
        }
    });

But using PUT method does not send the information to the control since doing dd ($ request- > all ()) shows me the blank array

function saveReg() {
var datos = new FormData();
datos.append('Date', $('#Date').val());
datos.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
    type: 'PUT',
    url: '{{url('/emp/newReg')}}',
    data: datos,
    contentType: false,
    processData: false,
    success: function (data) {
        // location.reload();
    },
    error: function (data) {
        console.log(data);
        showAjaxErrors(data, 'error_msg');
        markError(['Date', 'file']);
    }
});
    
asked by gmrYaeL 23.11.2018 в 16:25
source

2 answers

0

The AJAX documentation says:

The type of request to be made ("POST" or "GET"); the default value is "GET". Note: other HTTP request methods, such as PUT and DELETE, can also be used here, but are not compatible with all browsers.

What I recommend is to use GET and POST, since everything can be done with those 2 methods.

    
answered by 23.11.2018 в 17:15
0

First PUT is for specific url:
if we want to create a user example:
the url of the request must be specifically for the element
link
while for POST it can simply be link
since POST will send the data to the server only and already on the server it is decided what to do with them:

  • PUT puts a resource at the address specified in the URL. Exactly in that direction. If it does not exist, it creates it, if there is it replaces.
  • POST sends data to a URL so that the resource in that URI will drive.
  • and the biggest differences:

    Una petición PUT a una URL afecta únicamente a esa URL. Un POST a una URL puede tener cualquier efecto.
    En una petición PUT, los datos incluidos en el cuerpo de la petición se toman como una entidad que quedará accesible en la dirección URL de la petición.
    Solo debemos utilizar PUT cuando sabemos exactamente la url correspondiente al recurso que vamos a crear (o actualizar).
    PUT es idempotente, POST no es idempotente.
    

    Now, once we understand this, we are going to Laravel with a simple example:

    <form action="{{ route('route_name') }}" method="post">
        {{ method_field('PUT') }}
        {{ csrf_field() }}
    </form>
    

    this will produce this result:

    <form action="{{ route('route_name') }}" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </form>
    

    As there is no metedo put ... send a post method with a hidden input with name _method and value put

    now with jquery: although there are methods PUT and DELETE are not compatible with all browsers

        
    answered by 23.11.2018 в 17:35