3

I am having trouble implementing the Twitter Bootstrap Typeahead feature with a JSON web Service.

I have looked at all the examples on this website and can't find anything that will work. I know my JSON web service works; I can enter the address in my browser and it returns the JSON I expect with the MIME type set to "application/json".

I have this JavaScript in the

<body>

of my HTML page:

<script>
    $('#typeahead').typeahead({
        source: function (query, process) {
            return $.getJSON(
                'http://localhost:8732/IngredientsService/search/',
                { query: query },
                function (data) {
                    return process(data);
                });
        }
    });
</script>

I have this code as my "input":

<input type='text' id='typeahead' class='typeahead' data-provide="typeahead" data-items="10" />

Can anyone explain why this is not working?

David Poxon
  • 2,435
  • 4
  • 23
  • 44

3 Answers3

3

According to the last comment ("web service not being accessed") - have you tried the more normal / documented approach?

$('#typeahead').typeahead({
  source: function (query, process) {
     return $.get('http://localhost:8732/IngredientsService/search/', { query: query }, function (data) {
       return process(data.options); //if JSON is [ "options" : { ...} 
     });
  }
});

$.getJSON is not nessecary, but useful when you want to load an entire JSON and inject potion of it as a source for the typeahead

$.getJSON("http://localhost:8732/IngredientsService/search/", function(json) {
  $("#typeahead").typeahead({
    source : json.options //again, dont know the structure of the JSON
  });
});
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • No this hasn't worked. Does the Service URL expect a parameter? I.e. should I be mapping my Service method to a value form the URL? – David Poxon May 26 '13 at 13:56
  • 1
    I guess your index / calling script is not http://localhost:8732/ ?? It may be the [link](http://en.wikipedia.org/wiki/Same_origin_policy) Same Origin Policy that is your problem. – davidkonrad May 26 '13 at 15:58
  • Is there a way of resolving issue? – David Poxon May 27 '13 at 04:26
  • Yes, use a [proxy](http://stackoverflow.com/a/16495740/1407478) or try [JSONP](http://stackoverflow.com/a/2686406/1407478) – davidkonrad May 27 '13 at 10:23
0

From what I can see from the typeahead documentation, the source key in the parameters object is not valid.

It should look more like this I think:

 $('#typeahead').typeahead({
     remote: 'http://localhost:8732/IngredientsService/search/q=%QUERY' 
 });

http://jsfiddle.net/qVjsu/

0

It's better to omit return when using async sources because process() will be triggered twice

$('#typeahead').typeahead({
    source: function (query, process) {
        $.getJSON('/search/json/'+ query), function (data) {
            return process(data);
        });
    },
    minLength: 1,
    items: 8
});
Dionysios Arvanitis
  • 1,133
  • 9
  • 11