我试图使用Joomla 1.5在ajax中获取json对象但没有成功。阅读一些谷歌页面我遵循的方式似乎是这样做的,但是firebug上的javascript控制台返回Error: undefined, Status: parsererror
。
代码如下:
客户端:
$(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);
}
})
});
服务器端(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);
}
调用de ajax时,会显示Error: undefined, Status: parsererror
,并显示错误参数消息。
缺少什么作品?
答案 0 :(得分:1)
我不知道这是否是最好的答案,可能不是,因为我刚开始使用Joomla开发。我看到我必须捕获应用程序以将请求发送回ajax调用,因此在服务器端声明全局变量$mainframe
解决了这个问题。
function exchangeExamMonitorData()
{
global $mainframe;
// original code
$mainframe->close();
}