开发者

jQuery autocomplete - data.split is not a function

I am using jQuery plugin autocomplete in web application and i pass simple json data to the au开发者_如何学运维tocomplete. When i pass data this way, by the variable words. Autocomlete works fine.

var words = ["benzina","best","benátská noc","bez realitky","beroun","bershka","bernard","beskydy","belgie","berlin"];
$(document).ready(function() {
  $("#suggest").autocomplete(words, {
    formatItem: function(data, i, n) {
      return data[0];
    },  
      width: 342
  });
});

But when I download it from server I get this error message "data.split is not a function". I also set header Content-type: application/json in my server side script. I also try set option in autocommplete dataType: 'json', but still the same problem. When i parse data in autocomplete option parse: function(data) { .. } it looks, that data are OK, but i cannot read it in formatItem. Do you have got any idea?

$(document).ready(function() {
  $("#suggest").autocomplete("ajax.php?gsug="+whisp_id, {
    formatItem: function(data, i, n) {
      return data[0];
    },  
      width: 342
  });
});


Assuming you use this autocomplete: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

This autocomplete expects for remote data not an array, it expects a string, each entry in a separate line.

So what you can do: Return the expected response or build your own parse-function for the request(can be setup as an option).
This function has to return an array with objects, where the objects have the members data,value,result (take a look at the original parse-method in autocomplete.js to see what is what)

Furthermore: Be sure that ajax.php output's a valid json-string(use php's json_encode if you don't do yet). And set the dataType to 'json', like you already tried before.


because that url is just string not a json data, first you need to get the data.

try this way

$(document).ready(function() {
    $.ajax({
      url: "ajax.php?gsug="+whisp_id,
      cache: false,
      success: function(jsondata){
          $("#suggest").autocomplete(jsondata, {
            formatItem: function(data, i, n) {
              return data[0];
            },  
              width: 342
          });
      }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜