ComeRun
ComeRun

Reputation: 921

jQuery Autocomplete, source JSON through API

I am not completely familiar with the jQuery autocomplete so I hope you can give me some idea about how I am going to achieve my task.

I have a the following url:

http://localhost/contactApi.do?mobile=614321

at which the mobile can be whole or part of a contact number. I designed this API call a while a go and When you put this url in a browser and enter, you get a JSON response displayed on the front end with all the matching results (all mobiles like this number you enter in the url).

My question is how can I use (or can I?) this url as of my autocomlete source?

Like if I want to use something like the example given by jQuery UI demos, how can I pass the mobile to the url?

I have seen some places they have used such a source like the following:

source:"http://localhost/contactApi.do?mobile=?"

Now how does it work, this ? will pick what I type everytime or what?

Any help would be greatly appreciated

Upvotes: 1

Views: 1777

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can look at something like

$("input").autocomplete({
    source: function (request, response) {
        $.getJSON("http://localhost/contactApi.do?mobile=614321", 
          {  }, 
          function(data) {
              if(data){
                  response(data);
              }
          }
        );        
    }
});

The data should be array like [{value: <value>, text: <display-text>}, ....].

You can also look at the following samples
why jquery autocomplete doesnt work on https

Upvotes: 2

Related Questions