There is an ajax script that sends data to the server. This script returns two different strings, but in one variable:

.done(function(data){ console.log(data); }); 

How can I send one line with the data key, and the second with the response key, for example? I want to display one in the console as technical information, and use the second for my own purposes and not show it in the console.

For example:

 <?php //какой-то код echo json_encode($somedata); // data //какой-то код var_dump($another_data); ?> 

I want to display this data with different keys and work with them differently.

UPD:

Added code:

 echo json_encode(array( "technical_data" => $post_data, "user_data" => $response ), JSON_UNESCAPED_UNICODE); 
  • 2
    At the very least, it would not hurt us to see what the console.log call vp_arth does after all
  • @Other added code. - JamesJGoodwin

1 answer 1

Make the server return JSON, for example

 <?php $somedata = "hello"; $another_data = "world"; $response = array("data" => $somedata, "debug" => $another_data); echo json_encode($response); 

In the handler, get

 { "data":"hello", "debug":"world" } 

And refer to the field you need

  • Please review the added code. data.technical_data to access the data.technical_data element returns the string undefined . - JamesJGoodwin
  • 2
    @JamesJGoodwin At the very least, it would not hurt us to see what the console.log call out is after all -
  • Most likely JSON.parse needed JSON.parse after preliminary cleaning of the extra output) - vp_arth