這裏是我的控制器的方法:Symfony2的JSON響應返回奇怪的UTF字符
public function sendjsonAction()
{
$message = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Message')
->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('message' => new
JsonEncoder()));
$message = $serializer->serialize($message, 'json');
return new JsonResponse($message);
}
這裏是我的路由器:
acme_store_sendjson:
pattern: /sendjson/
defaults: { _controller: AcmeStoreBundle:Default:sendjson}
這裏就是我得到的,當我去/ sendjson /:
"[{\u0022id\u0022:1,\u0022iam\u0022:1,\u0022youare\u0022:2,\u0022lat\u0022:50.8275853,\u0022lng\u0022:4.3809764,\u0022date\u0022:{\u0022lastErrors\u0022:{\u0022warning_count\u0022:0,\u0022warnings\u0022:[],\u0022error_count\u0022:0,\u0022errors\u0022:[]},\u0022timezone\u0022:{\u0022name\u0022:\u0022UTC\u0022,\u0022location\u0022:{\u0022country_code\u0022:\u0022??
(與此類似)
我試圖做一個Ajax調用(使用jQuery)有以下幾點:
$.getJSON('/app_dev.php/sendjson', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
我也得到一個
Uncaught TypeError: Cannot use 'in' operator to search for '1549' in [{"id":1,...
當我改變的Symfony2的響應類型,我得到的名單
[對象] [對象] [對象] [對象] [對象] [對象] ...
我做錯了什麼?我應該解析答案轉換\ u0022到「或者是我從一開始就響應故障
編輯
我還可以通過改變控制器的嘗試:?
public function sendjsonAction()
{
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$message = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Message')
->findAll();
$serializer = $serializer->serialize($message, 'json');
return new Response($serializer);
}
這一次我得到了有效的JSON,(根據Jsonlint)buuttt頭不是應用程序/ JSON ...(有道理,因爲我發送一個響應而不是JsonResponse ...)(但多數民衆贊成我試圖避免,因爲JsonResponse似乎要改變添加奇怪的字符)
[{"id":1,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":2,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":3,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":4,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":5,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":6,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"}]
你的PHP似乎罰款,\ u0022是一個引號。請調試你的JavaScript,錯誤應該在你的js代碼中的某處。 – 2013-02-10 14:56:15
js是正確的,直接從jquery.com,它工作時,我手動內聯數據在數組中,併發送他們作爲迴應,所以問題是JSON ...我不應該有引號爲\ u0022字符..(沒有js那裏 - 那是Symfony的頁面) – 2013-02-10 15:01:22
嘗試新的響應($ message)。你得到了什麼? – 2013-02-10 15:11:20