Get Json string from $ _POST in Yii2

0

I try to make a query with $ .post from JQuery doing the following:

function consultaAjax(url, val){
    arreglo = { id: val };
    datos = JSON.stringify(arreglo);
    $.post( 
            url, 
            datos, 
            function(data){$("select#documentos-expedicion").html( data );}
    );}

and in the controller I have the following code

public function actionGetinfo(){

    $json = Yii::$app->request->post();
    $data = json_decode($json[0], true); // <- aqui esta el problema

    echo 'fuck '.$data; // esto no importa, solo me siento frustrado
}

$ json is a one-dimensional array containing the JSON string, (this I know thanks to the Netbeans debugger) the problem when I try to access the array is that it throws the error:

$data = $json[0]; // error "Undefined offset: 0"
$data = $json[1]; // error "Undefined offset: 1"
$data = $json['id']; // error "Undefined index: id"

If I pass it completely to "json_decode ()" it tells me that I expected a json string and that I passed an array and obvious "error".

How can I access the arrangement? Is there a procedure that is skipping me ?, and thanks in advance for the help.

    
asked by Arkham_alone 01.07.2017 в 00:27
source

1 answer

0

I have solved the problem after a lot of work.

The main problem arises because "google chrome" the browser under which I am developing the application, loads the assets of the application and does not update them unless the cache memory is erased with tools such as "ccleaner" or Disable the "cache" option in the developer options.

  • right click on the page select inspect item .
  • in the window that opens click on the three vertical points in the upper right corner.
  • click on Settings .
  • search for Network and select the option " Disable cache (while DevTools is open ) ".
  • After these steps and as long as the developer tool is open Chrome will always load the Js, Css and other assets when the page is refreshed.

    In addition to this I show you how the final code is in case someone else needs it.

    function consultaAjax(url, val){
        datos = { id: val };
        $.post( url, 
                datos, 
                function(data){$("select#documentos-expedicion").html( data );}
        );}

    PHP driver ...

    public function actionGetcites(){
    
        $id = Yii::$app->request->post('id');
    
        $cities = Municipios::find()->where(['departamento' => $id])->all();
    
        if($cities > 0){
            foreach($cities as $city){
                echo "<option value='".$city->id."'>".$city->nombre."</option>";
            }
        }
        else{
            echo "<option>No hay ciudades que mostrar</option>";
        }}
    
        
    answered by 03.07.2017 в 02:35