How to transform the JSON_ENCODE from PHP to JSON in javascript

-2

Hello,

My javascript pulls the result of a select done in PHP, which arrives in Javascript is as follows:

Idonotunderstandwhyit'scomingupwiththis"string ()" on the front,

I've already tried using JSON.parse, but it does not work, as in the image below,

The question is: How do I transform this string into JSON?

    
asked by anonymous 12.10.2018 / 02:44

2 answers

0

You're probably doing this in your PHP code:

var_dump($variavel);

What you need to do is:

echo $variavel;
    
13.10.2018 / 05:07
0

In the context of your php

$suaVariavel = json_encode($seu_Obj_ou_Arr);
print($suaVariavel);

In the Javascript context

$.getJSON(url.php, parans, function(respJson){
   // Aqui você acessa seu json já no formato de objeto.
   // Coloca isso --- respJson.nome --- em qualquer lugar que desejar.
   // E assim por diante.
});

PS: Do not forget to call jQuery.js on your page to run getJSON

    
13.10.2018 / 05:33