douhaodang0403 2011-12-01 03:23
浏览 135
已采纳

使用JQuery AJAX和php从mysql数据库中获取数据

This is follow up post where I'm having a problem where my php code is not returning the data it should. I have this api.php code (under Joomla):

<?php
require_once ( 'includes/defines.php' );
require_once ( 'includes/framework.php' );

/* Create the Application */
$app = JFactory::getApplication('site');

/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
    die("Access denied: login required.");

//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();

$query_camera_name = "SELECT camera_name, camera_status, camera_quality, email_notice, camera_hash, camera_type FROM #__cameras WHERE user_id=".$user->id." AND camera_status!='DELETED'";
$db->setQuery($query_camera_name);
//get number of cameras so we can build the table accordingly
$db->query();
$num_rows = $db->getNumRows();
// We can use array names with loadAssocList.
$result_cameras = $db->loadAssocList();
header('Content-Type: application/json');
echo json_encode($result_cameras);
?>

This code on its own returns valid JSON code. Then I have my client.php code that is there to display some results.

<html>
<head>
<link href="ajax_dashboard/webcam_widget.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
</head>
<body>

<h3>Output: </h3>
<div id="output">Append this text</div>

<script id="source" language="javascript" type="text/javascript">
var js = jQuery.noConflict();

js(function () 
{
js.ajax({                                      
  url: 'ajax_dashboard/api.php',                  //the script to call to get data          
  data: "",                        
  dataType: 'json',                //data format      
  success: function(data, textStatus, xhr) {
    console.log(xhr);
    js.each(data, function() {

       js.each(this, function(k, v) {
           js('#output').append("<b>key: </b>"+k+"<b> value: </b>"+v);

    });

   });

} 
});
}); 
</script>
</body>
</html>

I have verified with help from users on another post that the jquery code is just fine (http://stackoverflow.com/questions/8329495/iterate-over-json-array-using-jquery). The error I'm getting is from jquery

object is null

The extra console message gives this as well: Object { readyState=4, status=200, statusText="OK"} and responseText=""

For some reason the JSON code is not coming through right. Here is what happens when I look at the JSON code for those who want to verify:

[
{
    "camera_name": "ffgg",
    "camera_status": "DISABLED",
    "camera_quality": "MEDIUM",
    "email_notice": "DISABLED",
    "camera_hash": "0d5a57cb75608202e64b834efd6a4667a71f6dee",
    "camera_type": "WEBCAM"
},
{
    "camera_name": "test",
    "camera_status": "ENABLED",
    "camera_quality": "HIGH",
    "email_notice": "ENABLED",
    "camera_hash": "6ab000ef7926b4a182f0f864a0d443fc19a29fdd",
    "camera_type": "WEBCAM"
}
]

I'm thinking it has something to do with the way Joomla is displaying this. Any ideas?

  • 写回答

3条回答 默认 最新

  • dsj83686 2011-12-05 00:45
    关注

    Decided to answer my own question. It is just not possible to do this. I have to go the component route (create your own custom component). As far as I can see there is just no other way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题