簡體   English   中英

從 Codeigniter 控制器返回 Uploadive Ajax '錯誤/成功' 數據

[英]Returning Uploadifive Ajax 'Error/Success' data from Codeigniter Controller

我正在使用Uploadive上傳插件。 它可以正常工作。 但是,我很難通過插件將errorsuccess消息從控制器傳遞回我的視圖。 我正在記錄控制台並log錯誤。 但似乎無法將任何控制器錯誤返回給 Uploadive 調用以顯示在我的視圖中。

簡而言之,我想將errorsuccess消息從我的控制器(通過$result var)輸出回我的 ajax 函數以嵌入到我的視圖中。

警報alert(data.result)輸出“未定義”。

JS函數:

$('#file-upload').uploadifive({                 
    ...

    'onUpload': function(file) {
        console.log('upload complete!');
    },
    'onUploadComplete' : function(file, data) {
        console.log('The file ' + file.name + ' uploaded successfully.');

        // returned error/success message here
        alert(data.result);

    },
    'onError': function(errorType) {
        console.log(errorType);
    }
});

CI控制器方法:

function add_document() {

    // If Ajax request, proceed:
    if(isset($_POST['is_ajax'])) {

        if (!$this->upload->do_upload()) {
            
            // If file upload failed or is invalid, 
            // display error notification
            $result = array('error' => $this->upload->display_errors());
            echo json_encode($result);
        }       
        else {
            
            // If file upload was successful
            $result = 'success!';
            echo $result;

            ...
        }
    }
    // if not, redirect to upload page
    else {
        redirect('upload');
    }

}

我想出了如何讓它與我的控制器代碼一起工作。 Uploadifive 沒有很好的文檔記錄,所以這是一個不穩定的旅程。 但無論如何,這就是我如何讓它按照我想要的方式運行。 希望這對其他人有幫助。

在我的控制器中,我替換了echo json_encode($result); 與下面的一致。 指定我想使用的數組鍵(即錯誤)並將結果輸出為html

echo html_entity_decode($result['error']);

在我的 javascript 函數中,我只需要輸出data ......警報中不需要.result 請注意,我決定將結果附加到一些html而不是警報。

// Display Error/Success Messages
$('.status').html(data);

暫無
暫無

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

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