duan1930 2018-09-05 15:16
浏览 156

响应类型正在从JSON更改为HTML,而不进行任何代码更改

I have a legacy application in CakePHP 2.x

It has a method in a Controller that outputs JSON in a structure like this:

{"id":59,"name":"Association of Southeast Asian Nations (ASEAN)","n_grouptags":1}

The controller method used $this->response->type('json'); to set the content-type in the response to application/json; charset=UTF-8. All good.

What I noticed is that if the returned data becomes over a certain length, the content-type is set to text/html; charset=UTF-8 without any changes to the code.

I've included some screenshots below which show this, including the response type.

Small amount of data (content type = application/json - expected):

enter image description here

enter image description here

More data (content type = text/html - unexpected):

enter image description here

enter image description here

In both cases I have checked that the JSON is valid using https://jsonlint.com/

Why is this? Does it depend on the length of the response as to how the browser treats it, or is this a CakePHP problem?

The PHP that is responsible for the output is as follows - but no changes have been made to this between the 2 different outputs given above:

    $this->autoRender = false; // No View (template) is associated with this

    $out = []; // Reset

    // $tags is some data from a model
    foreach ($tags as $k => $v) {
        $n_grouptags = 123; // Actual number comes from a Model 
        $out[] = ['id' => $k, 'name' => $v, 'n_grouptags' => $n_grouptags];
    }

    $this->response->type('json'); // We *want* a JSON response

    echo json_encode($out, JSON_FORCE_OBJECT); // Encode $out (the output) as JSON

Caching within the application is disabled: Configure::write('Cache.disable', true);

  • 写回答

1条回答 默认 最新

  • dtpf76658 2018-09-05 15:41
    关注

    Controller actions are not supposed to echo data, even though it might work in some, maybe even most situations. The correct way of outputting data that doesn't stem from a rendered view template, is to configure and return the response object (or a string, but that's not forward compatible with 3.x), or to use serialized views.

    The root problem is not the length of the content, but generally outputting data before the response object can send headers, which will cause them to be ignored, this will happen once there's even a single byte being sent before the response emitter comes into play.

    It most likely only happens as of a certain length because you're using PHPs output buffering and/or compression (see output_buffering and zlib.output_compression in your php.ini), which will cause the echoed data to be held back until the buffer storage capabilities are being exceeded (in most cases that's usually 4096 bytes), or the buffer is explicitly flushed (which will happen automatically at the end of the script execution).

    tl;dr, for a quick fix, configure and return the response:

    $this->response->body(json_encode($out, JSON_FORCE_OBJECT));
    return $this->response;
    

    See also

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?