2012-12-05 39 views
1

我已經閱讀了一些非常類似於SO的問題,但沒有一個解決方案適用於我。CodeIgniter給了我錯誤404,但我可以在Firebug中看到響應

的問題是,例如,使用此代碼視圖:

$.ajax({ 
url: 'periodista/json', 
async: false, 
dataType: 'json', 
success: function (json) { 
$.each(json, function(k,v){valores.push(v); }); 

} 

我得到一個404 Not Found錯誤,但螢火蟲顯示我的答覆(http://i.imgur.com/ yG9cW.png)

我試圖讀取從的file_get_contents函數的簡單的PHP腳本的URL響應,但它不工作

$url="http://localhost/xampp/populus/wordpress/app/periodista/json"; 
$json = file_get_contents($url); 

我得到相同的答案(404錯誤,我可以閱讀中的回覆螢火蟲)。

我已經嘗試使用「index.php」URLs,沒有index.php的URL與htaccess的幫助,並使用完整的網址,但沒有任何工作。而且我沒有使用wordpress。

我想控制器沒有加載,當我嘗試訪問視圖,但我不知道爲什麼。

編輯:我的控制器功能代碼(我用點燃的數據表庫):

public function json() 
{ 
    $datatables = new Datatables(); 
    $datatables->select('nro_boletin')->from('proyecto_de_ley'); 
    echo $datatables->generate(); 


    $data['title'] = "Periodista"; 
    $this->load->view('periodista/json', $data); 

} 
+0

你可以在你的控制器顯示代碼? – Stanley

+0

@ user1877722請在您的問題中添加該代碼(通過編輯),而不是作爲註釋 –

+0

您不應該加載視圖。嘗試'$ this-> output-> set_content_type('application/json') - > set_output(json_encode($ data));' – Stanley

回答

0

嘗試設置內容類型JSON和輸出它,而不是加載視圖,如:

public function json() { 
    $datatables = new Datatables(); 
    $datatables->select('nro_boletin')->from('proyecto_de_ley'); 
    echo $datatables->generate(); 


    $data['title'] = "Periodista"; 
    $this->output 
       ->set_content_type('application/json') 
       ->set_output(json_encode($data)); 
    //OR 
    header('Content-Type: application/json',true); 
    echo json_encode($data); 

} 

由於你正在使用阿賈克斯,你的功能被稱爲

$.ajax({ 
    url: 'periodista/json', <-- your controllers function 
    data: {"test" : "test" }, 
    async: false, 
    dataType: 'json', 
    success: function (json) { //output from your json function 
      $.each(json, function(k,v){valores.push(v); }); 
    } 
+0

如何在不加載視圖的情況下調用此函數? – user1877722

+0

你不需要加載視圖,你的控制器函數(json)是從你使用的ajax中調用的 –

+0

好吧,現在這很奇怪。如果我輸入periodista/json,我可以看到json數據,但只有在Chrome中,在Firefox中,它給我找不到文件。 – user1877722

相關問題