我正在向我的api请求从服务器获取一些JSON数据,但似乎UTF-8字符未正确解码。刀片视图没有问题,但我不想使用blade.My标题是:
Cache-Control:no-cache
Charset:UTF-8
Connection:keep-alive
Content-Type:application/json
Date:Mon, 31 Mar 2014 19:02:41 GMT
Server:nginx/1.4.7
Set-Cookie:laravel_session=eyJpdiI6IkFFWExRQjJjc1wvYngwbm5jUnV2R2tiQ1l4eE5ObmpIMVwvYVFHbEsxSXByTT0iLCJ2YWx1ZSI6InY2emRrMWZ4WkxIRGplZ1ZqOWIyaWp1VHFpU2ZpYlwvTmh4aURUd20yTzgwdjZwUXBOVWZkZmoydDB0KzJFOXhoUHR6dkoyaUptYVZnd0duNGNCQjhVQT09IiwibWFjIjoiMDZhMmMyOTk4NjEzMTk5ZjIwYjE0Yjc4MTdiOGZlYjUyYmQ5OGQ2ZDM2M2NiNzdkZTVkZWIxNGUxY2Q4ZTQxNCJ9; expires=Mon, 31-Mar-2014 21:02:41 GMT; Max-Age=7200; path=/; httponly
Transfer-Encoding:chunked
X-Frame-Options:SAMEORIGIN
X-Powered-By:PHP/5.5.10
我的控制器是:
public function getIndex(){
$posts = Post::with('author','thumbnail')->where('id','<','150')->orderBy('created_at','DESC')->take(5)->get();
$response = Response::json($posts);
$response->header('Content-Type', 'application/json','');
$response->header('Charset','UTF-8');
return $response ;
}
以下是我要澄清的一个示例:
comments = [{
"id":149,
"user_id":0,
"title": "özlem güzelharcan"
},{
"id":239,
"user_id":1,
"title": "Özlem Güzelharcan",
}];
如何预编码我的json数组以正确获取土耳其语转义字符? 感谢
答案 0 :(得分:0)
通过阅读Laravel代码,这似乎是json function
:
// vendor/laravel/framework/src/Illuminate/ Support/Facades/Response.php
public static function json($data = array(), $status = 200, array $headers = array(), $options = 0)
{
if ($data instanceof ArrayableInterface)
{
$data = $data->toArray();
}
return new JsonResponse($data, $status, $headers, $options);
}
所以我认为你能够将你的标题作为数组传递给第3个参数,但我认为它带有正确的Content-Type和Charset。
是的,这是合并的拉取请求:
https://github.com/laravel/laravel/commit/d3ed53d48fb3429dfc2ce5e83d2022fc32acd4cd
----编辑----
清理你的json:
$j = '[{
"id":"149",
"user_id":"0",
"title":"özlem güzelharcan"
},{
"id":"149",
"user_id":"0",
"title":"Özlem Güzelharcan"
}]';
你必须解码这些html实体:
$j = json_decode(html_entity_decode($j, ENT_QUOTES, 'UTF-8'));
var_dump($j);