getJSON not working with typeahead
Bootstrap: 3.3.7
jQuery: 1.11.3
Hi Team,
I am facing an issue with using typeahead with a ajax response. My input field looks like below,
- <input class="form-control typeahead" name="referredBy" value="" type="text" id="referredBy" data-provide="typeahead" autocomplete="off">
my js looks like below;
- $('.typeahead').typeahead({
- hint: true,
- highlight: true,
- minLength: 2
- }, {
- source: function(query, process) {
- var url = "not putting in the actual url"
- matches = []
- teamMemberData = "";
-
- var execAjaxEmployeeLookup = function() {
- var dfd = $.Deferred();
- $.getJSON(url, function(data) {
- dfd.resolve(data);
- });
- return dfd.promise();
- }
-
- $.when(execAjaxEmployeeLookup()).then(function(data) {
- console.log("Employee Lookup GET Response: " + JSON.stringify(data));
-
- teamMemberData = "";
- for (var i = 0, len = data.teammembers.length; i < len; i++) {
- var areEqual = data.teammembers[i].lastname.substring(0, queryLength).toUpperCase() === query.toUpperCase();
- console.log(data.teammembers[i].lastname.substring(0, queryLength).toUpperCase() + " " + query.toUpperCase());
-
- if (areEqual) {
- teamMemberData = data.teammembers[i].lastname + " " + data.teammembers[i].firstname + " " + data.teammembers[i].workphoneext + " " + data.teammembers[i].company;
- matches.push(teamMemberData);
- }
- }
-
- process(matches);
- });
- });
I am getting a proper response from the AJAX request. I received a JSON file that will look like below;
- {
- "status": "Success",
- "teammembers": [
- {
- "id": "123",
- "lastname": "Pata",
- "firstname": "Chris",
- "workphoneext": "",
- "company": "Test Company"
- },
- {
- "id": "456",
- "lastname": "Pate",
- "firstname": "Corn",
- "workphoneext": "12345",
- "company": "Test Company"
- },
- {
- "id": "789",
- "lastname": "Patel",
- "firstname": "Hein",
- "workphoneext": "",
- "company": "Test Company"
- }
- }]
- }
I am pretty sure I am making mistake in cleansing data. I don't know what format I have to send back to typeahead as response. I am trying to concatenate a set values and pushing it to an array and then sending it as response to typeahead. But I don't see a the list as typeahead on input field.
Please let me know your thoughts on it. Thanks in advance!