Joomla中带有ajax的500“内部服务器错误”
问题描述:
I'm using Ajax at front-end joomla site but unfortunately it doesn't run. Here is my code:
/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();
}
}
?>
I get this error:
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)
I have read different posts about that, but nothing works....help please!
UPDATE
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;
}
UPDATE DEBUG
If I go to
index.php?option=com_prova&task=ajaxraw.updateReserve&format=raw&robotId=2&reserved=1&lang=en
I have this error
Invalid controller: name='ajaxraw', format='raw'
Someone can help me?
答
The following code I've fixed up for you will not be the solid solution but will push you in the right direction as there's currently a lot wrong with your code as it stands:
- Incorrect use of API
- Deprecated functions
- Incorrect approach for calling functions
Comment out all your Ajax code and let's just try and get some results show just with 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;
}
}