Home > database >  how to get json_encode php in javascript
how to get json_encode php in javascript

Time:05-25

I want to send this $data

if($query == true)
{
   
    $data = array(
        'status'=> true,
        'result' => 1
       
    );
}

echo json_encode($data);

then I want to retrieve the data

success: function(data) {
    console.log(data.status);
}

In console is undefined, but if I console.log(data), the data is called.

CodePudding user response:

Either use JSON.parse()

Or in your ajax request add:

dataType: 'json'

CodePudding user response:

You need to parse json data to use in javascript.

success: function(data) {

  if ( typeof data !=="undefined" ) {
     var result = JSON.parse(data);
     console.log(result);
  }

}

CodePudding user response:

your data might still be a string, and you need to parse it to object first. You can use JSON.parse.

let json = '{"test":"val"}';
console.log(JSON.parse(json).test); // will output val
  • Related