![](/img/trans.png)
[英]Using JQuery AJAX and php to fetch data to a mysql database
[英]Using JQuery AJAX and php to fetch data from a mysql database
這是跟進帖子,在我遇到問題的情況下,我的php代碼未返回應有的數據。 我有這個api.php代碼(在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);
?>
此代碼自行返回有效的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,status = 200,statusText =“ 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展示此內容的方式有關。 有任何想法嗎?
你能擺脫這個:
data: "",
從ajax調用,看看會發生什么? 我從來沒有見過它是空的,並且好奇是否會改變任何東西。 如果jQuery在發布URL后面附加了多余的內容,那么Joomla和您的php可能會感到沮喪,因為那是存在的。
在包含Joomla框架和其他文件之前, 必須定義JEXEC變量,因為所有其他文件在繼續之前檢查是否已定義此變量。
這是他們執行的檢查:
defined('_JEXEC') or die('Restricted access');
如此處所述: http : //docs.joomla.org/Why_do_most_of_the_Joomla!_PHP_files_start_with_%22defined%28%27_JEXEC%27%29...%3F
您需要將此行添加到文件中:
define( '_JEXEC', 1 );
但是警告一詞“ DANGER WILL ROBINSON,DANGER”
調用主Joomla入口點並將請求路由到您自己的組件可能會更安全。 這相當容易做到,您可以取消Joomla的所有無關緊要的輸出,並僅調用您自己組件的輸出,將布局指定為html,json,xml或所需的任何其他格式。
決定回答我自己的問題。 只是不可能這樣做。 我必須走組件路線(創建自己的自定義組件)。 據我所知,別無選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.