這是後續的帖子,我遇到了問題,我的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