最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI autocomplete via ajax error: - Stack Overflow

programmeradmin1浏览0评论

I've been searching for days now, but can't find a fix.

Here's my code (shortened to the core functionality):

$("input").autoplete({
    source: function( request, response ){
        $.ajax({
            url: 'inc/ajax.php',
            type: "GET",
            async: true,
            dataType: "json",
            data: {
              'task' : 'tasktodo',
              'squery' : request.term
            }, 
            success: 
                function( data ) {
                    response($.map( data, function(item){
                        return {
                            label : item['name'],
                            value : item['name']
                        }
                    }));
                }
        });     
    }
});

The autoplete does work, but I'm getting the following error in my browser's console:

 Uncaught TypeError: Object  has no method 'results' (in Chrome)
 TypeError: this.options.messages.results is not a function (in Firefox)

The error points to a line in jqueryui.js, which is called by "response()" in my script.

Even though the error doesn't affect the functionality I'd like to know why it's there.

I've been searching for days now, but can't find a fix.

Here's my code (shortened to the core functionality):

$("input").autoplete({
    source: function( request, response ){
        $.ajax({
            url: 'inc/ajax.php',
            type: "GET",
            async: true,
            dataType: "json",
            data: {
              'task' : 'tasktodo',
              'squery' : request.term
            }, 
            success: 
                function( data ) {
                    response($.map( data, function(item){
                        return {
                            label : item['name'],
                            value : item['name']
                        }
                    }));
                }
        });     
    }
});

The autoplete does work, but I'm getting the following error in my browser's console:

 Uncaught TypeError: Object  has no method 'results' (in Chrome)
 TypeError: this.options.messages.results is not a function (in Firefox)

The error points to a line in jqueryui.js, which is called by "response()" in my script.

Even though the error doesn't affect the functionality I'd like to know why it's there.

Share Improve this question edited Aug 26, 2013 at 9:34 Keren Caelen 1,4663 gold badges17 silver badges40 bronze badges asked Aug 26, 2013 at 9:17 Philipp WajeedPhilipp Wajeed 671 silver badge7 bronze badges 4
  • Could it be that data is null? In this case map fails – Stefan Commented Aug 26, 2013 at 9:21
  • can you share the relevant html also – Arun P Johny Commented Aug 26, 2013 at 9:23
  • 3 it looks like you are enabling an experimental API called live region extension, adding a messages` option` somehow... not able to recreate from my end... – Arun P Johny Commented Aug 26, 2013 at 9:30
  • 1 Oh wow, it was really that simple.. deleting the "messages" option solved it. Thanks Arun!! – Philipp Wajeed Commented Aug 26, 2013 at 9:44
Add a ment  | 

3 Answers 3

Reset to default 2

Very old question, but still relevant today as it happened me and I'm not sure the accepted answer covers all bases, or explains the problem.

This happens because the autoplete plugin expects you to provide a messages object with noResults and results properties for telling it how to label search results.

The noResults property should be a string that is displayed when, you've guessed it, there are no results.

The results property then should be a method that accepts a count parameter, and returns a string.

Something like this:

$("input").autoplete({
    source: function( request, response ){
        ... your $.ajax request stuff
    },
    messages: {
        noResults: "No results",
        results: function(count){
            return count + (count == 0 ? ' result' : ' results');
        }
    }
});

It's possible that calling response(data); avoids the need to set these properties at all. But in my case, a colleague had provided a messages object, but had set both noResults and results properties to string values, hence the error:

this.options.messages.results is not a function

try something like this

success: function(data){
   response(data);

}

I think there is a bug in response data map function , you might have to map like

response($.map( data.d, function(item){
return {
                                    label: item.name,
                                    value: item.name
                                }
发布评论

评论列表(0)

  1. 暂无评论