简体   繁体   English

Laravel:无法设置JSON utf-8字符集

[英]Laravel: Cant set JSON utf-8 charset

I have a specific problem in Laravel . 我在Laravel中有一个特定的问题。

When I use echo or print_r or var_dump my data (retrieved from eloquent) are printed in the right format (UTF-8, greek language). 当我使用echoprint_rvar_dump ,以正确的格式(UTF-8,希腊语言)打印我的数据(从雄辩的中检索出来)。 But when I use 但是当我使用

return Response::json($data)

returns something like 返回类似

"\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03bf\u03af \u03c7\u03b1\u03c1\u03b1\u03ba\u03c4\u03ae\u03c1\u03b5\u03c2"

I have already set UTF-8 as default charset in my php.ini , mysql.conf (but this is not the problem). 我已经在我的php.inimysql.conf中将UTF-8设置为默认字符集(但这不是问题)。

I have tried several ways to set the charset like: 我尝试了几种设置字符集的方法,例如:

Response::json($data,200,$headers)

where $headers is an array with: 其中$ headers是具有以下内容的数组:

'Content-type'=> 'application/json; charset=utf-8'

But nothing changes. 但是什么都没有改变。

My operating system is Debian 7 wheezy 64bit, PHP version 5.5.13 , MySQL 5.6.19 and apache2 server. 我的操作系统是Debian 7 wheezy 64bit,PHP版本5.5.13,MySQL 5.6.19和apache2服务器。

JSON_UNESCAPED_UNICODE didn't work without the right header set like this for me: 没有为我设置正确的标头, JSON_UNESCAPED_UNICODE无法正常工作:

return Response::json($data, 200, array('Content-Type' => 'application/json;charset=utf8'), JSON_UNESCAPED_UNICODE);

After the header Content-Type was set to application/json;charset=utf8 it did work for me. 将标题Content-Type设置为application/json;charset=utf8它确实对我application/json;charset=utf8

Works with current Laravel Version. 与当前的Laravel版本一起使用。 (5.4) (5.4)

From Laravel 4.1, you can specify json_encode() options as the 4th parameter on the method below. 在Laravel 4.1中,您可以在以下方法中将json_encode()选项指定为第4个参数。 You will want to pass the JSON_UNESCAPED_UNICODE option to escape the utf-8 text 您将需要传递JSON_UNESCAPED_UNICODE选项来转义utf-8文本

Response::json($data, 200, $headers, JSON_UNESCAPED_UNICODE);

This function will pass the options through when it calls json_encode() 此函数在调用json_encode()时将传递选项

http://php.net/manual/en/function.json-encode.php http://php.net/manual/en/function.json-encode.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM