I'm trying to get json object in ajax using Joomla 1.5 without success. Reading some google pages I followed what appear to be the way to do that, but javascript console on firebug returns Error: undefined, Status: parsererror
.
The code follows:
Client side :
$(document).ready(function() {
$.ajax({
url: "http://localhost/courses/2015/ppc/index.php?option=com_exammonitor&task=exchangeExamMonitorData",
data: {
'first_name': "{TOKEN:FIRSTNAME}",
'last_name': "{TOKEN:LASTNAME}",
'exam_name': "{SURVEYNAME}"
},
dataType: "json",
type: "POST",
error: function(xhr, status, errorThrown) {
alert("Ajax error!");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
},
success: function(data){
console.log(data);
}
})
});
Server side (controller.php):
function exchangeExamMonitorData()
{
$user =& JFactory::getUser();
//TODO: verificar existência e permissão de usuário
$post = JRequest::get('post');
$firstName = $post['first_name'];
$lastName = $post['last_name'];
$examName = $post['exam_name'];
$model =& $this->getModel('exammonitor');
$result = $model->exchangeExamMonitorData($firstName, $lastName, $examName);
$response = array("success" => true, "result" => $result);
// Get the document object.
$document = JFactory::getDocument();
// Set the MIME type for JSON output.
$document->setMimeEncoding('application/json');
echo json_encode($response);
}
When de ajax is called, it displays Error: undefined, Status: parsererror
, and displays the error parameter message.
What piece is missing?
I don't know if it is the best answer, probably not, since I've just started with Joomla development. I saw that I have to capture the application to send request back to ajax call, so declaring the global variable $mainframe
in server side solved the problem.
function exchangeExamMonitorData()
{
global $mainframe;
// original code
$mainframe->close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.