quickquestion quickquestion - 1 year ago 119
Javascript Question

json_encode returning as undefined

So I've been reading up on a few guides on using json to return values to JS from php. I've however encountered a bit of a problem. Right now, I've got a jquery-function (don't get distracted with 'id: 1', it's used for something else), such as:

$('#some_trigger').on('change', function(){
$.post("retrieve.data.php", {
id: 1
}, function (data) {
var values = data;


And on the receiving end (in 'retrieve.data.php'), I've got:

$arr_assoc_template = array(
"name" => "aName",
"id" => "anId"

echo json_encode($arr_assoc_template, JSON_PRETTY_PRINT);

However, the console and alert window keeps logging [name] as undefined. What am I missing? :(

Answer Source

Use JSON.parse, turns a string of JSON text into a Javascript object.

var values = $.parseJSON(data);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download