使用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?

doubaomao9304
doubaomao9304 是的,这是对的。
接近 9 年之前 回复
dongyuan2388
dongyuan2388 你确定ajax_dashboard/api.php引用了实际的文件名吗?
接近 9 年之前 回复

3个回答



决定回答我自己的问题。 这是不可能做到的。 我必须去组件路线(创建自己的自定义组件)。 据我所知,没有别的办法。</ p>
</ div>

展开原文

原文

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.



你可以摆脱这个:</ p>

  data:“”,
来自ajax调用的</ code> </ pre>

看看会发生什么? 我从来没有看到过那么过去,如果那会改变什么就会好奇。 Joomla和你的php可能会因为jQuery用额外的东西附加帖子URL而感到不安,因为那里存在。 </ p>
</ div>

展开原文

原文

Can you get rid of this:

data: "",

from the ajax call and see what happens? I've never seen that passed as empty and would be curious if that would change anything. Joomla and your php might get upset if jQuery is appending the post URL with extra stuff because that is there.

duanqiechui2378
duanqiechui2378 当我删除它时仍然是同样的问题。
接近 9 年之前 回复
duanmao1975
duanmao1975 data是一个可选参数,所以我想把它留空会产生同样的效果。
接近 9 年之前 回复



在包含Joomla框架和其他文件之前,必须</ em>定义JEXEC变量,因为所有其他文件都会检查 这个变量在继续之前定义。</ p>

这是他们执行的检查:</ p>

  defined('_ JEXEC')or die(' 限制访问'); 
</ code> </ pre>

,如下所述: http://docs.joomla.org/Why_do_most_of_the_Joomla!_PHP_files_start_with_%22defined%28%27_JEXEC%27%29...%3F </ p>

您需要将此行添加到您的文件中:</ p>

  define('_ JEXEC',1); 
</ code> </ pre>

但是警告“危险将会罗宾逊,危险”</ p>

您可能会更安全地调用主要的Joomla入口点并将请求路由到您的 自己的组件。 这很容易做到,你可以抑制Joomla的所有外部输出,只调用你自己的组件输出,指定布局为html,json,xml或你需要的任何其他格式。</ p>
</ div>

展开原文

原文

Before you include the Joomla framework and other files you must define the JEXEC variable as all other files check whether this variable is defined before continuing.

This is the check they carry out:

defined('_JEXEC') or die('Restricted access');

as explained here: http://docs.joomla.org/Why_do_most_of_the_Joomla!_PHP_files_start_with_%22defined%28%27_JEXEC%27%29...%3F

You need to add this line to your file:

define( '_JEXEC', 1 );

But a word of warning "DANGER WILL ROBINSON, DANGER"

You would probably be much safer calling the main Joomla entry point and routing the request to your own component. This is fairly easy to do, you can suppress all of Joomla's extraineous output and call just your own component's output specifying the layout to be either html, json, xml or any other format you need.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐