这是后续的帖子,我遇到了问题,我的php代码没有返回它应该返回的数据。我有(下的Joomla)这个api.php代码:使用JQuery AJAX和PHP从mysql数据库获取数据
<?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);
?>
自身收益有效的JSON代码此代码。然后我有我的client.php代码在那里显示一些结果。
<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>
我有帮助的另一篇文章的jQuery代码就好了验证,从用户(http://stackoverflow.com/questions/8329495/iterate-over-json-array-using-jquery)。我得到的错误是从jQuery的
对象为空
额外的控制台消息给出了这样的还有:{对象的readyState = 4,状态= 200,状态文本= “OK”}和responseText =“”
出于某种原因,JSON代码没有通过正确的。这是发生了什么,当我看JSON代码,谁想要验证:
[
{
"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"
}
]
我想它是与Joomla是显示这种方式。有任何想法吗?
你确定'ajax_dashboard/api.php'指的是实际的文件名吗? – mc10
是的,它是正确的。 – Tom