2

i am a newbie and am stuck at retrieving json data.

following is my index.php :

<script type="text/javascript" src="jquery-1.3.2.min_2.js"> 
$("document").ready(function() {

    $.getJSON("data.php",function(jsondata){
    $("#content").html(jsondata[0].content);    

    });
});
</script>
</head>
<body>
<div id="content"></div>
<div class="lg"></div>
</body>

in my data.php i am using the standard way of encoding and sending the data:

// execute query 
        $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
         $row = mysql_fetch_row($result);
         $jsonserver[$i] = $row;
    }
    echo json_encode($jsonserver); 
    header('Content-type: application/json');
    mysql_free_result($result); 
    // close connection 
    mysql_close($con);  

i am using mysql database. when i open localhost/data.php the json data is shown on the browser. but in case if i open localhost/index.php i donot get any desired output. Please explain. Thanks!

4
  • 1
    What does json_encode($jsondata) output? Commented Mar 9, 2011 at 23:33
  • Is that the full code of index.php? Do you have an element with id="content"? Commented Mar 9, 2011 at 23:36
  • Uncaught ReferenceError: manifest is not defined this is the error i get in chrome on pressing ctrl+shift+i when i execute data.php, however firebug doesnot show anything of this sort in firefox. Commented Mar 9, 2011 at 23:50
  • You should send the response headers before the content. Also make sure PHP file doesn't have any characters or spaces before the <?php opening tag, because this will cause PHP to send the default headers most common would be: Content-type: text/html. Commented Mar 10, 2011 at 0:19

2 Answers 2

1

you need to put all headers before any 'echo's on the page

so:

header('Content-type: application/json');
echo json_encode($jsonserver); 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your effort, doesnt your suggestion doesn't seem to work.
0

Hey, u didn't close script tag properly

try

<script type="text/javascript" src="jquery-1.3.2.min_2.js"></script>
<script type="text/javascript">
$("document").ready(function() {
  $.getJSON("data.php",function(jsondata){
        $("#content").html(jsondata[0].content);
    });
});
</script>

2 Comments

also use console.log(jsondata) to debug
great observation, still what i feel the problem lies in data.php as well. chrome inspector gives the follwoing error:Uncaught ReferenceError: manifest is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.