0

I need to set Content-Type: application/json; charset=UTF-8 in a data view from CakePHP. I've already tried to set $this->response->header('Content-Type', 'application/json; charset=UTF-8'); but that doesn't change anything. It still just output Content-Type: application/json.

FrediWeber
  • 1,099
  • 1
  • 10
  • 21

6 Answers6

1

its in the code (CakeResponse, line 447):

if (strpos($this->_contentType, 'text/') === 0) {
    $this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
} else {
    $this->header('Content-Type', "{$this->_contentType}");
}

so only for "text/..." the charset will be appended. I dont know why, though...

mark
  • 21,691
  • 3
  • 49
  • 71
  • why looking for a workaround? either googling and finding out that other types have a "right" to also be appended with the charset and making this an official cakephp bug - or realizing that cakephp actually sets the headers properly according to the HTTP spec. both is possible. – mark Nov 16 '12 at 22:43
  • 1
    PS: My googling revealed: http://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean => "redundancy" for json anyway. about other types I don't know. – mark Nov 16 '12 at 22:45
  • for clarification: "Content-Type: application/json; charset=UTF-8" has been added to the core as fix for this in the current master branch: https://github.com/cakephp/cakephp/commit/9b479958f613e2ce1d290f26d246e76313e821f2 - the issue is officially solved – mark Nov 21 '12 at 13:29
  • It was, back then in 2012. – mark Nov 25 '15 at 18:04
0

Funny, I had this same problem just last night. It seems that UTF-8 is the default JSON encoding and that's probably why the header isn't sent by Cake (but I couldn't find reference to this decision either way).

Solution for me was to use a php's json_decode() -- it should decode the string properly. I had lots of French accents showing up as java/C unicode, e.g. \u00E9 and thought it was a header problem. But after running the string through json_decode() the accented characters all showed correctly.

Marks' link the comments has good info too.

Costa
  • 4,851
  • 33
  • 30
0

for this you can place this in the controller / function:

json_encode($updown_rs);

and in the view file (views/check_json):

var json_object = $.parseJSON(response);   
Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
archit
  • 61
  • 1
0

application/json doesn't have a charset parameter. There's no point in trying to set one. See http://rfc7159.net/rfc7159#ianacons.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
0

I´m using cake 2.10.11 and just used this code in the json layout:

$this->response->charset('utf-8');
Tiago Souza
  • 182
  • 1
  • 10
0

You can do it by using $this->RequestHandler->ext = 'json'; in your beforefilter method

check out the following example

public function beforeFilter(){
    parent::beforeFilter();
    $this->RequestHandler->ext = 'json';
}

public function index()
{
    $this->autoRender = false;
    $output = array(
      array('value' =>'first value'),
      array('value' =>'second value'),
    );
    $json = json_encode($output);
    $this->response->body($json);
}
Dey
  • 842
  • 9
  • 21