开发者

Return JSON data from PHP, and use it using Javascript

开发者 https://www.devze.com 2023-02-05 05:00 出处:网络
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

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开发者_Python百科":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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消