我在Zend框架2中使用了JQuery-Autocomplete 服务器的响应必须是遵循JavaScript对象的JSON格式:
[
{ value: 'Afghan afghani', data: 'AFN' },
{ value: 'Albanian lek', data: 'ALL' },
{ value: 'Algerian dinar', data: 'DZD' },
{ value: 'European euro', data: 'EUR' },
{ value: 'Angolan kwanza', data: 'AOA' },
{ value: 'East Caribbean dollar', data: 'XCD' },
]
这是我的ajax动作
public function autocompleteAction()
{
$Keyword = "c";
$product = $this->getProduitsTable()->getProduitComplete($Keyword);
foreach ($product as $item) {
$row['value'] = htmlentities(stripslashes($item->PROD_DESIGNATION));
$row['id'] = (int)$item->PROD_ID;
$row_set[] = $row;//build an array
}
$result = new JsonModel ($row_set);
return $result;
}
我得到了这个结果
{
"0": {
"value": "BIc",
"data": 3
},
"1": {
"value": "Eastpak Valise \u0026agrave; roulettes, 78 L, Multicolore- Stripe In",
"data": 4
},
"2": {
"value": "Rainex Rock\u0027s 100 Sous-chemises",
"data": 5
},
"3": {
"value": "Roller correcteur rechargeable - Pritt - 4,2mm",
"data": 6
},
"4": {
"value": "10 Craies de couleur - Elami - Assortiment",
"data": 7
},
"5": {
"value": "Cartouche jet d\u0027encre Office Depot Compatible HP",
"data": 11
},
"6": {
"value": "Cartouche jet d\u0027encre HP C4837A 11 Magenta",
"data": 12
},
"7": {
"value": "Moniteur LCD HP 23cw 58.4 cm (23\u0026quot;)",
"data": 13
},
"8": {
"value": "Unit\u0026eacute; centrale Lenovo 90BJ003TFR",
"data": 14
},
"9": {
"value": "Ensemble PC ASUS UC K31ADE-FR008T + Ecran VS228DE 54.6 cm (21.5\u0026quot;)",
"data": 15
},
"10": {
"value": "Ordinateur portable ASUS Premium R511LJ 39.6 cm (15.6\u0026quot;) 6 Go Windows 8.1 64 Bits",
"data": 16
},
"modulenamespace": "Docs"
}
并出现以下错误:
SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
如何让Rails以正确的格式生成JSON?
非常感谢
答案 0 :(得分:0)
Thank you Vikash Kumar :) the Problem was in js file
$(function(){
// setup autocomplete function pulling from currencies[] arra
$('#PROD_DESIGNATION').autocomplete({
//// Url:baseUrl+"/ajax/autocomplete", <--- The problem was here
serviceUrl:baseUrl+"/ajax/autocomplete",
onSelect: function (suggestion) {
///
}
});
});