从PHP返回JSON数据,并使用Javascript使用它
我处理过JSON数据,这是Jquery的getJSON方法,它使用PHP的服务器端脚本来获取数据.现在,我正在尝试处理由PHP函数返回的JSON数据,而Javascript事件或url将获得该数据.对于要访问PHP数据的Javascript函数,它必须具有回调,但我无法弄清楚应如何创建回调.
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.
我在PHP文件(getData.php)中拥有的现有代码可以回显/返回JSON输出,并将提供如下所示的数据
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"}]}
现在我有一个javascript文件可以拨打电话了,这将是一个类似于Twitter的
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>
现在,当我运行此html文件时,出现一条错误消息
Now when I run this html file, I get an error saying
错误:标签无效源文件: http://myserver/getData.php 第1行: 列:1源代码: {"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"}]}"
"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"}]}"
在错误控制台中.
我很高兴我能够将数据从php端获取到html文件,但是现在我不知道如何使用返回的JSON数据.我将如何开发可将JSON数据返回的回调函数?
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?
您会收到此错误,因为JSON的构造对于JavaScript解析器而言只是错误的.裸露的花括号({
)后,Javascript要求声明.
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.
您的代码似乎期望的是,将JSON 不仅仅作为<script>
包含在页面中,而是由XMLHttpRequest(即"ajax")主动获取. .这确实很常见,并且与<script>
标记提供的内容完全不同.在这种情况下,JSON结构将由客户端代码显式解析,并用于所需的任何事情.
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.