Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have been searching the web like crazy for a day straight trying to figure out why my javascript code is not working. I am trying to pass an array from PHP to Javascript using JSON. After that i want to use it in other functions, thus kinda making the array or variable global. But i have been unable to get it to work, here is my code so far:

data = [];
$(document).ready(function() {
  $.getJSON('database.php', function(phpdata){
    // vad du vill göra här, allt retuneras i data-variabeln
    console.log(phpdata);
    data[0] = phpdata[0];
    console.log(data[0]);
  });

console.log(data);

Any ideas?

share|improve this question

marked as duplicate by adeneo, Josh Mein, Salvador Dali, David, Josh Crozier Nov 22 '13 at 2:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
what is the output of console.log(phpdata);? –  Matthew Graves Nov 21 '13 at 22:50
    
is database.php echoing a json object? If so, are the headers correct on database.php? –  NaNpx Nov 21 '13 at 22:53
5  
it's asynchronous ! –  adeneo Nov 21 '13 at 22:56
    
Please post the code to database.php, so that we can help you. –  Connor Gurney Nov 21 '13 at 23:00

2 Answers 2

When $.getJSON is called, the JS engine will step parallely to the next instruction, which here is console.log(data);. This behaivour is caused to $.getJson's asynchronuality.

Since the next step is calculated and executed in the very next millisecond, the webpage was not fully requested and data is still on its initialized value ([]).

What you can do:

Put all instructions that have to do something with your data in another function and call it on the anonymous function function(phpdata) { ... }.

Like this:

function dataLoaded(data) {
   // show data in dom elements
   // work with data, e.g. check for data["error"] = true or show the username from data["username"]
}

data = [];
$(document).ready(function() {
  // Load JSON from server, wait for it and then work with it in dataLoaded(..)
  $.getJSON('database.php', function(phpdata){
      dataLoaded(phpdata);
  });
}
// this is wrong:
// console.log(data);

It is required that database.php will return an JSON array in the JSON data type. See json_encode and header("CONTENT-TYPE: application/json") on the php manual.

Thanks to the comment for saying: failed case sensitive and data type.

share|improve this answer
1  
That should be header('Content-Type: application/json') (note that PHP instructions are case-sensitive). –  Marcel Korpel Nov 21 '13 at 23:06
    
You are very right! –  Dennis Ziolkowski Nov 21 '13 at 23:07
    
Just one little thing: There's no such thing as a "JSON Object". –  Marcel Korpel Nov 21 '13 at 23:09
    
I get that point :-) Didn't talk about an object, just about "JSON array" what is not technically and definitionally right, too. –  Dennis Ziolkowski Nov 21 '13 at 23:12
    
I tryed this, but i doesnt seem to work anyways =/. –  Bobbie Nov 21 '13 at 23:37

What does database.php do? It should probably be outputting a JSON encoded string.

http://us3.php.net/json_encode

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.