我在前端joomla网站上使用Ajax,但遗憾的是它并没有运行。这是我的代码:
/components/com_prova/js/dashboard.js
$.ajax({
url: "index.php?option=com_prova&task=ajaxraw.updateReserve&format=raw",
data: { robotId: idRobot, reserved: book}
}).done(function(response) {
console.log(response);
});
/components/com_prova/controllers/ajaxraw.php
<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.controller');
jimport('functions.php');
class MycomponentControllerAjaxraw extends JController
{
function updateReserve(){
$booked = JRequest::getVar('reserved');
$robotId = JRequest::getVar('robotId');
$db_external=db_ext();
$query = $db_external->getQuery(true);
$query = 'UPDATE robots SET booked='.$booked.' WHERE id='.$robotId;
$db_external->setQuery($query);
$db_external->query();
}
}
?>
我收到此错误:
jquery-1.12.3.min.js:4 GET http://xxxxxxxx/xxxxxxx/index.php?option=com_prova&task=ajaxraw.updateReserve&format=raw&robotId=1&reserved=1&lang=it 500 (Internal Server Error)
我已经阅读了不同的帖子,但没有任何作用......请帮忙!
更新
function db_ext(){
$option = array();
$option['driver'] = 'mysql'; // Database driver name
$option['host'] = 'xxxxxx'; // Database host name
$option['user'] = 'xxxxxxx'; // User for database authentication
$option['password'] = 'xxxxxxxx'; // Password for database authentication
$option['database'] = 'xxxxxxxxx'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$db_external = &JDatabase::getInstance( $option );
return $db_external;
}
更新调试
如果我去
?的index.php选项= com_prova&安培;任务= ajaxraw.updateReserve&安培;格式=生&安培; robotId = 2及保留= 1&安培;朗= EN
我有这个错误
Invalid controller: name='ajaxraw', format='raw'
有人可以帮助我吗?
答案 0 :(得分:1)
我为您修复的以下代码不是可靠的解决方案,但会让您朝着正确的方向前进,因为您的代码目前存在很多错误:
注释掉所有的Ajax代码,让我们尝试使用PHP显示一些结果:
class MycomponentControllerAjaxraw extends JController
{
public function updateReserve()
{
$input = JFactory::getApplication()->input;
$booked = $input->get('reserved', '', 'INT');
$robotId = $input->get('robotId', '', 'INT');
$db = $this->external_db();
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('booked') . ' = ' . (int)$booked
);
$conditions = array(
$db->quoteName('id') . ' = ' . (int)$robotId
);
$query->update($db->quoteName('robots'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
private function external_db()
{
$option = array();
$option['driver'] = 'mysql';
$option['host'] = 'xxx';
$option['user'] = 'xxx';
$option['password'] = 'xxx';
$option['database'] = 'xxx';
$option['prefix'] = '';
$db = JDatabaseDriver::getInstance($option);
return $db;
}
}