簡體   English   中英

在Joomla 1.5中通過json_encode調用從ajax獲取響應

[英]Get response from ajax with json_encode call in Joomla 1.5

我正在嘗試使用Joomla 1.5在Ajax中獲取json對象,但沒有成功。 閱讀一些谷歌頁面,我遵循了似乎是這樣做的方法,但是螢火蟲上的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 ,並顯示錯誤參數消息。

缺少哪一塊?

我不知道這是否是最佳答案,因為我剛開始進行Joomla開發。 我看到我必須捕獲應用程序才能將請求發送回ajax調用,因此在服務器端聲明全局變量$mainframe解決了該問題。

function exchangeExamMonitorData()
{
    global $mainframe;

    // original code

    $mainframe->close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM