CodeIgniter给了我错误404,但我可以在Firebug中看到响应

时间:2022-06-01 18:02:50

I have already read some questions very similar in SO, but none of the solutions worked for me.

我已经在SO中读过一些非常类似的问题,但没有一个解决方案适合我。

The problem is, for example, with this code in the view:

问题是,例如,视图中的代码:

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

}

I get a 404 Not Found error, but Firebug shows me the Response (http://i.imgur.com/yG9cW.png)

我收到404 Not Found错误,但Firebug向我显示了响应(http://i.imgur.com/yG9cW.png)

I've tried to read the url response from a simple php script with file_get_contents function but it doesn't work

我试图用一个简单的php脚本用file_get_contents函数读取url响应,但它不起作用

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

I get the same answer (404 error, and I can read the response in Firebug).

我得到了相同的答案(404错误,我可以阅读Firebug中的响应)。

I've tried using the "index.php" URLs, the URLs without index.php with help of an htaccess, and using the complete URLs but nothing worked. And I'm not using wordpress.

我已经尝试使用“index.php”URL,没有index.php的URL和htaccess的帮助,并使用完整的URL但没有任何效果。而且我没有使用wordpress。

I think the controller is not loaded when I try to access the view, but I don't know why.

我想当我尝试访问视图时没有加载控制器,但我不知道为什么。

Edit: My Controller function code (I'm using Ignited Datatables library):

编辑:我的控制器功能代码(我正在使用Ignited Datatables库):

    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);

}

2 个解决方案

#1


0  

try setting content type json and outputting it instead of loading view, like:

尝试设置内容类型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);

}

Since you are using ajax, your function is called

由于您使用的是ajax,因此调用了您的函数

$.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); });
    }

#2


0  

I had a similar issue and I had to set header manually.

我遇到了类似的问题,我不得不手动设置标题。

public function get_prev_input_data()
{
    header("HTTP/1.1 200 OK");
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");

    $userdata = [
        "id" => 5,
        "username" => "captain hook",
    ];

    $prev = [
        "a1" => 123,
        "a2" => 46,
    ];
    $response = [
        "userdata" => $userdata,
        "previnput" => $prev
    ];
    echo json_encode($response);
}

So you should add this line

所以你应该添加这一行

    header("HTTP/1.1 200 OK");

in your json() function.

在你的json()函数中。

#1


0  

try setting content type json and outputting it instead of loading view, like:

尝试设置内容类型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);

}

Since you are using ajax, your function is called

由于您使用的是ajax,因此调用了您的函数

$.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); });
    }

#2


0  

I had a similar issue and I had to set header manually.

我遇到了类似的问题,我不得不手动设置标题。

public function get_prev_input_data()
{
    header("HTTP/1.1 200 OK");
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");

    $userdata = [
        "id" => 5,
        "username" => "captain hook",
    ];

    $prev = [
        "a1" => 123,
        "a2" => 46,
    ];
    $response = [
        "userdata" => $userdata,
        "previnput" => $prev
    ];
    echo json_encode($response);
}

So you should add this line

所以你应该添加这一行

    header("HTTP/1.1 200 OK");

in your json() function.

在你的json()函数中。