How to return the correct content-type for JSON in CakePHP?

前端 未结 5 551
别那么骄傲
别那么骄傲 2021-02-04 13:24

I\'m trying to set the content-type header for a JSON response accessed with an AJAX GET request. I\'ve followed tutorials on blogs and the bakery but I always receive \'text/ht

5条回答
  •  时光取名叫无心
    2021-02-04 14:21

    After reading this and this, I got the following to return "Content-Type:application/json":

    Configure::write('debug', 0);
    $this->RequestHandler->respondAs('json');
    $this->autoRender = false;            
    echo json_encode($data);
    

    With JQuery's $.getJSON method, I'm still getting

    Resource interpreted as image but transferred with MIME type text/html.
    

    But at least my data is parsing.

提交回复
热议问题