从PHP返回JSON数据,并使用Javascript使用它 [英] Return JSON data from PHP, and use it using Javascript

查看:128
本文介绍了从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.

这篇关于从PHP返回JSON数据,并使用Javascript使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆