Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Sorry if this is still another thread on the subject but I am struggling since hours but could not find the solution. I am trying to get data from a Mysql database, create a JSON with php, then parse this JSON in javascript.

Here is my json.php

<?php

$link = mysql_pconnect("localhost", "root", "") or die("Could not connect". mysql_error());
mysql_select_db("people") or die("Could not select database");

$arr = array();

$rs = mysql_query("SELECT * FROM nom");

while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}
echo '{"users":',json_encode($arr),'}';

/*
//The json object is :
{"users":[{"id":"1","prenom":"Alain","age":"23"},{"id":"2","prenom":"Bruno","age":"24"}]} 
*/
?>

Then I try to parse it into java

<div id="placeholder6"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
    $.getJSON('http://localhost/json.php', function(data) {
    var output="<ul>";
    for (var i in data.users) {
        output+="<li>" + data.users[i].id + " " + data.users[i].prenom + "--" + data.users[i].age+"</li>";
    }

    output+="</ul>";
    document.getElementById("placeholder6").innerHTML=output;
});
</script>

when I replace localhost/json.php by the result in a file data.json, it works, when I open localhost/json.php with firefox, I can see the JSON table...so I do not know why it does not work with localhost/json.php. Is my php code or javascript code wrong ?

Thanks in advance for your help !

share|improve this question
    
Finally it worked, I had to add "header("Content-Type: application/json");" and "header("Access-Control-Allow-Origin: *");" to my php file. – user3833880 Aug 9 '14 at 14:57

3 Answers 3

Try This in php

while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

$return = new stdClass();
$return ->users = $arr;

echo json_encode($return);
share|improve this answer
    
Have you tried my code? – baxri Aug 9 '14 at 11:50
    
I tried of course. I guess it was json_encode and not jsob_encode. The json.php file does not show anything now when I open it with firefox. Is this normal ? – user3833880 Aug 9 '14 at 12:04

Try this method

var users= data.users;
$.each(users,function(index,users){
    console.log(users.prenom); /// and users.id etc
})
share|improve this answer
    
Hi, thanks for your answer. It does not work even if I replace the json.php by data.json. – user3833880 Aug 9 '14 at 11:46
    
var data= {"users":[{"id":"1","prenom":"Alain","age":"23"},{"id":"2","prenom":"Bruno","age‌​":"24"}]}; var users= data.users; $.each(users,function(index,users){ console.log(users.prenom); /// and users.id etc }) – Jithin Lukose Aug 9 '14 at 11:55
    
Just try this one. This is working for me – Jithin Lukose Aug 9 '14 at 11:56
    
Thanks. This does not work for me. I use "code.jquery.com/jquery-1.7.1.min.js";. what do you use ? – user3833880 Aug 9 '14 at 12:02
    
any error in console? try chrome. – Jithin Lukose Aug 9 '14 at 12:03

I think your web application server (like Apache or nginx) sends Content-Type: text/html by default or something of that sort for your json.php file. On the other hand, it looks like $.getJSON method requires a application/json content type field.

Try adding:

header("Content-Type: application/json");

to the top of the json.php file.

Edit - additional info:

I couldn't find in the original documentation of the $.getJSON method whether it, in fact, requires the specific Content-Type so I looked into the source code:

https://github.com/jquery/jquery/blob/1.7.1/src/ajax.js#L294

Here is the line of source code for jQuery 1.7.1 (which is the version you said that you use, I hope) for getJSON and as you can see, it calls jQuery.get with the last argument set to "json".

In turn, the jQuery.get documentation reveals that this argument means:

The type of data expected from the server. Default: Intelligent Guess (xml, json, script, or html).

from: http://api.jquery.com/jQuery.get/

Thus, when you call $.getJSON("/url/to/file", ...) that first argument is expected to be a JSON. If you add the PHP code from the top of my answer, your web application server will mask the output of the php file as a JSON.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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