1

I have worked with JSON data, Jquery's getJSON method to use PHP's server side scripts to get data. Now I am trying to work with JSON data being returned by a PHP function and a Javascript event or url would get that data. For a Javascript function to access PHP data, it has to have a callback, and I am not able to figure out, how I should create a callback.

Existing code that I have in the PHP file (getData.php) can echo/return JSON output and would give data that would look like this

{"count":3,"items":[{"myTab_ID":"1","myTab_xam":"test1","myTab_rank":"21"},
{"myTab_ID":"2","myTab_xam":"test2","myTab_rank":"22"},
{"myTab_ID":"3","myTab_xam":"test3","myTab_rank":"22"}]}

Now I have my javascript file which would be making the call, which would be a little twitter like,

 <html>
 <head>
 <script type="text/javascript" src="http://myserver/getdata.php">
 </script>
 </head>
 </html>

Now when I run this html file, I get an error saying

"Error: invalid label Source File: http://myserver/getData.php Line: 1, Column: 1 Source Code: {"count":3,"items":[{"myTab_ID":"1","myTab_xam":"test1","myTab_rank":"21"},{"myTab_ID":"2","myTab_xam":"test2","myTab_rank":"22"},{"myTab_ID":"3","myTab_xam":"test3","myTab_rank":"22"}]}"

in the error console.

I am a little happy that I atleast am able to get the data from my php side to the html file, but now I am wonder how would I use that returned JSON data. How would I develop the callback function that returns the JSON data back??

> ------code in getData.php---------------
> 
> $con =
> mysql_connect("localhost","peter","abc123");
> if (!$con) { die('Could not
> connect: ' . mysql_error()); }
> 
> mysql_select_db("my_db", $con);
> 
> $result = mysql_query("SELECT * FROM
> Persons");
> 
> while($row =
> mysql_fetch_array($result)) { 
> $data[] = $row; }
> 
> echo json_encode($data);
> 
> --------------end - getdata.php--------------
> 
> ---------code in getJson.html------------- html> <head>
> <script type="text/javascript"
> src="http://myserver/getdata.php">
> </script> </head>
> 
> </html>
> -----------------------end- getJson.html----------

I am not sure how a callback would work to get the data?

Pointy
415k62 gold badges597 silver badges631 bronze badges
asked Jan 14, 2011 at 16:12
2
  • When putting code in your questions, just indent each line by 4 spaces. Commented Jan 14, 2011 at 16:21
  • @Pointy - you can also just highlight it and click the {} button Commented Jan 14, 2011 at 16:32

3 Answers 3

2

Don't forget your header in getData.php:

header('Content-type: application/json');

Some useful examples in the php manual

answered Jan 14, 2011 at 16:21
2
  • When I add this header, the file is coming up for download. As soon as I load a page, I am getting a prompt for downloading the getData.php file. Is this wierd? Commented Jan 14, 2011 at 16:27
  • Is that when you are trying to view the output through a browser? There are various methods of getting browsers to interpret application/json - eg. adallow.wordpress.com/2008/10/13/… Commented Jan 16, 2011 at 17:23
1

You're getting that error because that JSON construction is simply erroneous to the JavaScript parser. After a naked open curly brace ({), Javascript expects a statement.

What your code seems to expect is that the JSON not be included simply as a <script> into a page, but instead that it be fetched actively by an XMLHttpRequest (i.e., "ajax"). That's really common, and it's quite different from what a <script> tag gives you. In such situations, the JSON construction is explicitly parsed by client-side code and used for, well, whatever is desired.

answered Jan 14, 2011 at 16:23
4
  • Correct; as-is, your HTML page is trying to include JSON as JavaScript, which fails. You need to assign it to a variable, either through an AJAX call, or the PHP page needs to output it as a variable (var myData = {...}). Commented Jan 14, 2011 at 16:38
  • how can I assign the output as a variable? do I just return the json_encode($data)?? or would I have write some javascript inside my php and return that?? Commented Jan 14, 2011 at 16:44
  • If the PHP code wrote the JSON out with a little Javascript around it, that would work fine, as others have noted. Just have the PHP code prefix that JSON with the JavaScript code to declare a variable, like var x = { json stuff here }; Commented Jan 14, 2011 at 17:01
  • Assume $data contains the json data, so do I do a echo '<script> var myData = "<?php $data; ?>"</script>'; Is this what you would suggest Pointy? Commented Jan 14, 2011 at 17:08
0

if you want the json encoded $data array to be avaliable in javascript you can do this:

<?php $jencodeddata = json_encode($data); ?>

in your HTML file..

<script type="text/javascript">
function doSOmething()
{ 
var jsencodedata = '<?php print $jencodeddata ?>';
you can loop through jsencodedata here....
}
</script>

hope this helps.

answered Jan 14, 2011 at 16:24
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.