處理 JSON Response 的中文編碼問題,在寫 json API 的時候常會碰到
一種做法是在 Response::json 裡面塞 Content-Type

$headers = array(
    'Content-Type' => 'application/json; charset=utf-8'
);

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

請不要另外寫一個 key 叫 charset,因為[http response header並沒有charset這個項目啊](http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields]

另一種做法是去 override Laravel framework 裡面的 json response function,位置在 \vendor\laravel\framework\src\Illuminate\Support\Facades\Response.php

詳細內容參考
Cant set JSON response utf-8 charset
让Json更懂中文(JSON_UNESCAPED_UNICODE)

同場加映: 混亂的 URLEncode

Comments

comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project