Laravel:无法设置JSON utf-8字符集 [英] Laravel: Cant set JSON utf-8 charset

查看:412
本文介绍了Laravel:无法设置JSON utf-8字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel中有一个特定的问题.

I have a specific problem in Laravel .

当我使用echoprint_rvar_dump时,以正确的格式(UTF-8,希腊语言)打印我的数据(从雄辩的中检索出).但是当我使用

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). But when I use

return Response::json($data)

返回类似

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

我已经在我的php.inimysql.conf中将UTF-8设置为默认字符集(但这不是问题).

I have already set UTF-8 as default charset in my php.ini, mysql.conf (but this is not the problem).

我尝试了几种设置字符集的方法:

I have tried several ways to set the charset like:

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

其中$ headers是具有以下内容的数组:

where $headers is an array with:

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

但是什么都没有改变.

我的操作系统是Debian 7 Wheezy 64位,PHP版本5.5.13,MySQL 5.6.19和apache2服务器.

My operating system is Debian 7 wheezy 64bit, PHP version 5.5.13 , MySQL 5.6.19 and apache2 server.

推荐答案

JSON_UNESCAPED_UNICODE在没有为我设置正确的标头的情况下不起作用:

JSON_UNESCAPED_UNICODE didn't work without the right header set like this for me:

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

将标题Content-Type设置为application/json;charset=utf8后,它确实对我有用.

After the header Content-Type was set to application/json;charset=utf8 it did work for me.

与当前的Laravel版本一起使用. (5.4)

Works with current Laravel Version. (5.4)

这篇关于Laravel:无法设置JSON utf-8字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆