problem when accessing data property of a get request in axios gives me error

0

I am making a request get with axios the problem is that when reading the property of the json I get undefined, I'm using php slim on the server as api, on the client side is a mobile app with nativescript-vue I leave the code to see what the error is

slim php code

$app->get('/getToken', function ($request, $response, $args) {
// CSRF token name and value
$nameKey = $this->csrf->getTokenNameKey();
$valueKey = $this->csrf->getTokenValueKey();
$name = $request->getAttribute($nameKey);
$value = $request->getAttribute($valueKey);


$arreglo=array("namekey"=>$name ,"valuekey"=>$value);
$response->write(json_encode($arreglo,JSON_UNESCAPED_SLASHES));
});

the code in the client

function GetToken(){
console.log("entramos en getToken");

       axios.get("http://tallerinternet.esy.es/getToken").then(respuesta=>{

                console.log(respuesta.data);

                console.log(respuesta.data.namekey);
      }).catch(function (error) {
           // handle error
          console.log("error " + error); 
        }); 
 }

when doing the console.log (answer.data) it shows me the structure of the json

{
  "namekey":"csrf5b9975d057d61",
  "valuekey":"63b5e0e720bffd5a8ccad0257d61ff62"
}

but when I try to print the property with console.log (answer.data.namekey) it gives me undefined,

I may be missing something when making the request with axios

in this link you will find what has an answer that comes from the server link but if I occupy the http module that comes by default in the data the slash does not appear.

    
asked by jose miguel jara 12.09.2018 в 22:54
source

1 answer

0

You have the problem in the JSON_UNESCAPED_SLASHES option of the json_encode , see how you bring it according to the code you shared here and see how they would be without that option:

var response = {
  data: {
         "namekey":"csrf5b998483cf2bc",
         "valuekey":"cfc55cd6bbf20704fe09472a6a1e986a"
        }
}

console.log(response)
console.log(response.data)
console.log(response.data.namekey)
    
answered by 13.09.2018 в 01:23