简体   繁体   English

使用zf2的自动完成Json响应格式

[英]Autocomplete Json response format with zf2

I used JQuery-Autocomplete with Zend framework 2 the Response from the server must be JSON formatted following JavaScript object : 我将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' },
  ]

This is my ajax Action 这是我的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;                                                             
 }         

I get this results 我得到这个结果

  {
  "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"
}

and this error in : 和此错误:

SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符

How do I get Rails to generate JSON in the correct format ? 如何获得Rails以正确的格式生成JSON?

Thank you very much 非常感谢你

Thank you Vikash Kumar :) the Problem was in js file 谢谢Vikash Kumar :)问题出在js文件中

 $(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) {

         /// 
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM