el_pup_le
el_pup_le

Reputation: 12179

Mystery ajax request occurring somehow

Whenever I type in the autocomplete field an ajax request is sent and there is no code I've written to do this. Checking the console I see it's a 400 GET request to the controller that loaded this view with param (json) appended to the url. I'm absolutely stumped.

<head>
<script data-main="<?=base_url()?>public/requirejs/main.js" src="<?=base_url()?>public/requirejs/require-jquery.js"></script>
<script>
    requirejs(['a_mod'],
        function(a_mod) {
            $(document).ready(function() {
                var param = [];
                param = $('#elem').attr('value');
                a_mod.foo(param, "#someElem");
            });
        });
<script>

main.js

require(["jquery",
         "jquery-ui"],
         function() {

         }
);

The autocomplete function

'foo' : function(param, elementAutocomplete, elementTags) {
        console.log("init ac");
        $(elementAutocomplete).autocomplete({
            source: param,
            minLength: 1,
            select: function (event, ui) {
                event.preventDefault();
                //
            }

        }).data( "autocomplete" )._renderItem = function( ul, item ) {
            return $("<li></li>")
                .data( "item.autocomplete", item )
                .append( '<a>' + item.label  + '</a>' )
                .appendTo(ul);
        }
    },

Upvotes: 1

Views: 62

Answers (1)

mu is too short
mu is too short

Reputation: 434635

Your source attribute for the autocompleter is a string:

param = $('#elem').attr('value');

And a string source means that it is a URL:

Autocomplete can be customized to work with various data sources, by just specifying the source option. A data source can be:

  • an Array with local data
  • a String, specifying a URL
  • a Callback

Saying var param = []; just means that param is initialized as an empty array, it doesn't mean that param will always be an array. You need to fix your param value to be an array.

Upvotes: 2

Related Questions