簡體   English   中英

AJAX腳本未在Codeigniter中返回成功

[英]AJAX script not returning success in codeigniter

JS:

$(function() {
    load_custom_topics()
//    load_main()
});

function load_custom_topics(){
    $.ajax({
    type: "POST",
    async: false,
    url: 'http://rickymason.net/thebump/index.php/ajax/load_custom_topics',
    dataType: 'json', 
    data: { },
        success: function(page){
            alert(page);
        }
    });
    event.preventDefault()
}

load_custom_topics

public function load_custom_topics()
{
    $check = $this->page_model->check_active_topic();
    if ($check == FALSE) 
    {
            $page['content'] = 'TEST equals FALSE'; 
    } else {
            $page['content'] = 'TRUE'; 
    }
    echo json_encode($page);
}

轉到頁面index.php / ajax / load_custom_topics返回以下內容:

{"content":"TEST equals FALSE"}

警報未觸發! 知道為什么嗎?

實際上,在檢查對控制器的請求時,我發現您沒有設置ajax調用期望的正確標頭(text / json)。

參見codeigniter的Output類。

使用

$this->output->set_content_type('application/json')->set_output(json_encode($page));

代替

echo json_encode($page);

應該可以。

暫無
暫無

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

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