getJSON not working with typeahead

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,

  1. <input class="form-control typeahead" name="referredBy" value="" type="text" id="referredBy" data-provide="typeahead" autocomplete="off">
my js looks like below;

  1. $('.typeahead').typeahead({
  2. hint: true,
  3. highlight: true,
  4. minLength: 2
  5. }, {
  6. source: function(query, process) {
  7. var url = "not putting in the actual url"
  8. matches = []
  9. teamMemberData = "";
  10. var execAjaxEmployeeLookup = function() {
  11. var dfd = $.Deferred();
  12. $.getJSON(url, function(data) {
  13. dfd.resolve(data);
  14. });
  15. return dfd.promise();
  16. }
  17. $.when(execAjaxEmployeeLookup()).then(function(data) {
  18. console.log("Employee Lookup GET Response: " + JSON.stringify(data));
  19. teamMemberData = "";
  20. for (var i = 0, len = data.teammembers.length; i < len; i++) {
  21. var areEqual = data.teammembers[i].lastname.substring(0, queryLength).toUpperCase() === query.toUpperCase();
  22. console.log(data.teammembers[i].lastname.substring(0, queryLength).toUpperCase() + " " + query.toUpperCase());
  23. if (areEqual) {
  24. teamMemberData = data.teammembers[i].lastname + " " + data.teammembers[i].firstname + " " + data.teammembers[i].workphoneext + " " + data.teammembers[i].company;
  25. matches.push(teamMemberData);
  26. }
  27. }
  28. process(matches);
  29. });
  30. });


I am getting a proper response from the AJAX request. I received a JSON file that will look like below;

  1. {
  2.     "status": "Success",
  3.     "teammembers": [
  4.         {
  5.             "id": "123",
  6.             "lastname": "Pata",
  7.             "firstname": "Chris",
  8.             "workphoneext": "",
  9.             "company": "Test Company"
  10.         },
  11.         {
  12.             "id": "456",
  13.             "lastname": "Pate",
  14.             "firstname": "Corn",
  15.             "workphoneext": "12345",
  16.             "company": "Test Company"
  17.         },
  18.         {
  19.             "id": "789",
  20.             "lastname": "Patel",
  21.             "firstname": "Hein",
  22.             "workphoneext": "",
  23.             "company": "Test Company"
  24.         }
  25. }]
  26. }

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!