ServerSide $_POST is empty

ServerSide $_POST is empty

mgbtttmgbttt Posts: 4Questions: 2Answers: 0

Hello.
I have configured the DataTable with ServerSide.

$("#tablaProductos").DataTable({
    "Processing": true,
    "serverSide": true,
    "ajax": {
        "url": "ajax/datatable-productos.ajax.php",
        "type": "POST"
    }
});

The network communication shows:
General

Request URL: http://localhost/POS_TH/ajax/datatable-productos.ajax.php
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade

Response Headers

Cache-Control: no-store, no-cache, must-revalidate
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 294
Content-Type: text/html; charset=UTF-8
Date: Fri, 22 Nov 2019 15:31:15 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=83
Pragma: no-cache
Server: Apache/2.4.34 (Ubuntu)
Vary: Accept-Encoding

Request Headers

Accept: application/json, text/javascript, /; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9,en;q=0.8,ca;q=0.7
Connection: keep-alive
Content-Length: 1569
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: P...
Host: localhost
Origin: http://localhost
Referer: http://localhost/POS_TH/productos
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
X-Requested-With: XMLHttpRequest

Form data

draw: 1
columns[0][data]: 0
columns[0][name]:
columns[0][searchable]: true
columns[0][orderable]: true
columns[0][search][value]:
columns[0][search][regex]: false
columns[1][data]: 1
columns[1][name]:
columns[1][searchable]: true
columns[1][orderable]: true
columns[1][search][value]:
columns[1][search][regex]: false
columns[2][data]: 2
columns[2][name]:
columns[2][searchable]: true
columns[2][orderable]: true
columns[2][search][value]:
columns[2][search][regex]: false
columns[3][data]: 3
columns[3][name]:
columns[3][searchable]: true
columns[3][orderable]: true
columns[3][search][value]:
columns[3][search][regex]: false
columns[4][data]: 4
columns[4][name]:
columns[4][searchable]: true
columns[4][orderable]: true
columns[4][search][value]:
columns[4][search][regex]: false
columns[5][data]: 5
columns[5][name]:
columns[5][searchable]: true
columns[5][orderable]: true
columns[5][search][value]:
columns[5][search][regex]: false
columns[6][data]: 6
columns[6][name]:
columns[6][searchable]: true
columns[6][orderable]: true
columns[6][search][value]:
columns[6][search][regex]: false
order[0][column]: 0
order[0][dir]: asc
start: 0
length: 10
search[value]:
search[regex]: false

The code in datatable-productos.ajax.php is: var_dump($_POST);
And the response is: array(0) { }

I has tried
"contentType": "application/json" in datatable configuration
and var_dump(file_get_contents("php://input"))
but nothing changes.

If I change the ajax type by GET and read $_GET I'll have all the parameters sended (draw, ...)
Why? How can I use POST type?

Thanks in advance

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin

    That's weird - your inspector's output shows that the POST is being sent with form data, so the problem must be with the server-side. Possibly PHP is configured not to allow POST input (although I can't imagine why that would be done).

    Have you tried getting the data from $_REQUEST rather than $_POST? Also, have you checked the server's error logs?

    Thanks,
    Allan

  • mgbtttmgbttt Posts: 4Questions: 2Answers: 0

    Hello Allan and thanks for your reply.
    Using var_dump($_REQUEST) return array(0) { } too.
    I have seen the logs of apache2 and any error are reported.

    The problem is with Chrome, because when I use Firefox var_dump($_POST) return the FormData.

    I don't understand why. Do you have some clue about what is happend?
    Thanks in advance.

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin

    Hang on - if you use Firefox it works okay. If you use Chrome you get nothing from $_POST?!

    But Chrome is showing that it is posting data in the network inspector?

    My best guess is that the server is doing some kind of filtering and dropping the data coming from Chrome. I've never heard of that being done before though!

    Are you able to make any POST requests to the server from Chrome (i.e. using just $.ajax(), not specifically DataTables)?

    Allan

  • mgbtttmgbttt Posts: 4Questions: 2Answers: 0

    Thank you Allan for your reply and time.

    Yes, I do a lot of ajax call with POST and there is no problem. For example, in order to create a code number.

    function crearCodigoProducto(){ 
        var fecha = moment().format('MMYYDD');
        var datos = new FormData();
        datos.append("fecha", fecha);
        $.ajax({
            url:"ajax/productos.ajax.php",
            method: "POST",
            data: datos,
            cache: false,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function(respuesta){
                var nuevoCodigo = respuesta ? Number(respuesta) +1 : fecha+"001"; 
                $("#nuevoProducto").val(nuevoCodigo);
            }
        })
    }
    

    And if I write var_dump($_POST) and see the response in the network inspector of Chrome shows

    array(1) {
      ["fecha"]=>
      string(6) "111928"
    }
    

    I don't understand why.
    What is the lifecycle of datatable with serverside?
    If you construct a datatable with serverside and put var_dump($_POST) in the server ajax code the inspector of Chrome shows you anything?

    In this exaple the differences in the header are:

    Response Headers:
    - There isn't Content-Encoding: gzip
    - There isn't Vary: Accept-Encoding
    Request Headers:
    - Accept-Language: en-US,en;q=0.9
    - Cache-Control: no-cache
    - Content-Type: multipart/form-data; boundary=----WebKitFormBoundary0DS4B5LeB2D4PHPc
    - Pragma: no-cache

    All this is because I'm debugging my code and I can't know if the data arrive to the server.

    Thanks for your

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin

    What happens if you try:

    function crearCodigoProducto(){ 
        var fecha = moment().format('MMYYDD');
        $.ajax({
            url:"ajax/productos.ajax.php",
            method: "POST",
            data: { 'fecha': fecha },
            cache: false,
            dataType: "json",
            success: function(respuesta){
                var nuevoCodigo = respuesta ? Number(respuesta) +1 : fecha+"001"; 
                $("#nuevoProducto").val(nuevoCodigo);
            }
        })
    }
    

    ?

    i.e. rather than using FormData, just use jQuery's data option directly.

    Thanks,
    Allan

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    This thread has been duplicated here - please post only once to avoid this confusion.

This discussion has been closed.