简体   繁体   English

自动完成JSON响应不起作用

[英]Autocomplete JSON response not working

im getting response in json (can check in firebug), but this wont parse the json response and no results displayed. 我在json中获取响应(可以在Firebug中检入),但这不会解析json响应,并且不会显示任何结果。 what mi doing wrong? 我在做什么错? i could'nt find anything on doc http://docs.jquery.com/Plugins/Autocomplete 我在doc http://docs.jquery.com/Plugins/Autocomplete上找不到任何内容

Here is my JSON response 这是我的JSON回应

({"Contacts":[{"Phone":"","Email":"","Labels":"","Mobile":"12345678","Firstname":"john"}]});

And This is my jQuery: 这是我的jQuery:

$("#destinations").autocomplete({
    source: function (request, response) {
        $.getJSON("http://localhost/contactApi.do?callback=?", 
          { 'contactMobile': request.term, maxRows: 12, style: "full" }, 
          function(data) {
              if(data.Contacts){
                  var x = $.map(data.Contacts, function(v, i){
                      console.log(v)
                      return {
                          label: v.Mobile + ' - ' + v.Firstname, 
                          v: v.Firstname
                      }
                  });
                  response(x);
              }
          }
        );        
    }
})

It happened to me once that the problem was on the server side. 一旦碰到问题出在服务器端,我就想到了。 I was sending the response as a raw string instead of JSON. 我将响应作为原始字符串而不是JSON发送。 Take a look in firebug if you are able to see the proper headers (content-type:application/json) being sent back from the server. 如果您能够看到从服务器发回的正确标头(content-type:application / json),请看一下Firebug。 Also you should be able to see a tab named JSON using firebug. 另外,您还应该能够使用firebug查看名为JSON的选项卡。 After I added the proper headers I was able to deserialize the values using jQuery. 添加适当的标头后,我可以使用jQuery反序列化值。

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

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