Because in php you can directly access the variables sent in an object from ajax

1

Does anyone know why in php you can directly access the variables sent by ajax inside an object without using the function json_decode () of php ?. This is the function that sends the value "value":

function mi_funcion() {
    var valor = "mi_valor";
    $.ajax({
        url: "mi_archivo.php",
        type: "POST",
        async: true,
        //data: "valor="+valor,
        data: {"valor": valor},
        dataType: "json",
        success: function(respuesta) {
            $("#mi_div").text(respuesta["valor"]);
        },
        error: function() {
            $("#mi_div").text("Error");
        },
        timeout: 60000
    });
}

And this is the php part:

<?php
$resultado = array();
$resultado["valor"] = $_REQUEST["valor"];
echo json_encode($resultado);
?>

PS: The above works well, so my question is why it is accessed in the same way in php for the following two cases: 1. data: {"mtdo": mtdo}, and 2. data: " mtdo="+ mtdo, If in one case it is an object and in the other it is a chain.

    
asked by Antonio Perez 17.05.2016 в 22:42
source

2 answers

2

Taken from the jQuery documentation:

  

data
  Type: PlainObject or String or Array
  Data to be sent to the server. It is converted to a query string, if not already to string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key / Value pairs. If value is an Array, jQuery serializes multiple values with the same key based on the value of the traditional setting (described below).

To summarize a little what it says (and what matters to us in this case), jQuery will convert the information of data to a string, in case it is not yet.

In the jQuery code we can also see when you convert it: link

    // Convert data if not already a string
    if ( s.data && s.processData && typeof s.data !== "string" ) {
        s.data = jQuery.param( s.data, s.traditional );
    }

You can see what jQuery.param does.

On the php side, we see that we are simply going to receive a regular POST, with the serialized data, and the use of the variable 'superglobal' $_REQUEST will contain the POST data, as explained by the php documentation:

Documentation php: $ _REQUEST

  

$ _ REQUEST - HTTP Request Variables
An associative array that by default contains the contents of $ _GET, $ _POST and $ _COOKIE.

     

This is a 'superglobal' or a global automatic variable. It simply means that it is a variable that is available anywhere in the script. No need to make global $ variable; to access it from functions or methods.

    
answered by 18.05.2016 / 00:37
source
1

If you comment on a php object that is saved in this case in the variables $_REQUEST and $_POST since the request is being made by the post method, now you may be confusing the object that weapons in javascript in the ajax with what the php receives in the server.

Greetings

    
answered by 17.05.2016 в 23:07