0

simple thing.

$.getJSON is used and the result from the .php file being called is caught as response in the jquery callback function. The json encoded php array is an associative array.

$.getJSON("file.php",function(response){ // print the associative response array in var_dump or print_r fasion }); 

file.php contains:

<?php $my_array=array(); //$my_array['random_index1']="random_value1"; //$my_array['random_index2']="random_value2"; //$my_array['random_index3']="random_value3"; // and so on echo json_encode($my_array); ?> 

$my_array has random keys and values.

How can I print the whole 'response' array just like the print_r fashion in php?

EDIT1: I want to print it on the webpage or in an alert box. Not that i just want to watch the values in the javascript console(chrome, FF or whatever).

EDIT2: if I write the body of $.getJSON as follows: why won't it work:

 for(var i in response){ console.log("i="+i+" content="+response[i]); } 

4 Answers 4

1

I believe console.dir() is what you're looking for:

https://developer.mozilla.org/en-US/docs/Web/API/Console.dir

The only downside is it doesn't allow for labeling each object that's output, and it's also a non-standard console method.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have Chrome or Firebug, you can use console.log(response). In the console, you can click on the logged object to view the properties of it.

Comments

0

Use something like Doug Crockford's JSON library to transform the response to text and log it with console.log

function(response) { console.log(JSON.stringify(response)); } 

Comments

0
$.getJSON("file.php", function(data) { var items = []; $.each(data, function(key, val) { items.push('<li id="' + key + '">' + val + '</li>'); }); $('<ul/>', { 'index': 'my-list', html: items.join('') }).appendTo('body'); }); 

Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.

IF you desire an alternative format you could modify the each such as:

 $.each(data, function(key, val) { alert('key:' + key + ' value:' + val); }); 

I admit I borrowed from: http://api.jquery.com/jQuery.getJSON/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.