admin管理员组

文章数量:1426072

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?

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?

Share Improve this question edited Jan 14, 2011 at 16:21 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Jan 14, 2011 at 16:12 machamacha 7,49719 gold badges65 silver badges85 bronze badges 2
  • When putting code in your questions, just indent each line by 4 spaces. – Pointy Commented Jan 14, 2011 at 16:21
  • @Pointy - you can also just highlight it and click the {} button – Parris Varney Commented Jan 14, 2011 at 16:32
Add a ment  | 

3 Answers 3

Reset to default 2

Don't forget your header in getData.php:

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

Some useful examples in the php manual

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 mon, 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.

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.

本文标签: Return JSON data from PHPand use it using JavascriptStack Overflow