0

Here's the long and short of it. I am trying to get data from a dropdown and textbox, send them via JSON AJAX to query a search index and return a dictionary-like object. The object is being returned from the server fine, but I am unsure of how to return it to the function it was called from.

Here is the calling function:

//Checks search form to ensure fields are populated.  If populated calls the appropriate function to search either Contact or Client index.
//Receives result from server and calls another function to upload search API data to searchTable.
function searchClient()
{
    var searchFor = document.getElementById("searchFor").value;
    var searchTerm = document.getElementById("searchTerm").value;

    if (searchFor=="Search For..." || searchTerm==""){
        document.getElementById("searchValidateLabel").innerHTML = "Please ensure that all required fields are populated.";
    } else {
        document.getElementById('searchTableScroller').style.display = '';
        document.getElementById("searchValidateLabel").innerHTML = "";
        var searchResults = getSearchResults(searchFor, searchTerm);
        console.log(searchResults);

        if (searchFor == "Client"){
            resetSearchClientTable();
            populateClientSearchTable(searchResults);
        }
        else if (searchFor == "Contact"){
            resetSearchContactTable();
            populateContactSearchTable(searchResults);
        }
    }
}

Here is the JSON function:

function getSearchResults()
{
    var jsonData = JSON.stringify({
        searchFor: searchFor.value, 
        searchTerm: searchTerm.value, 
    });

    $.ajax({    
    url: '/json_client_search',
    type: 'POST',
    contentType: 'application/json',
    data: jsonData,
    success: function(response) {
        console.log(response);
        return (response);
    },
    error : function(xhr,errmsg,err) {
        bootbox.alert(xhr.status);
    }
    });
}

Here is the result returned via console.log, but is not returning to the original calling function. When I console log it there it is undefined.

search.SearchResults(results=[search.ScoredDocument(doc_id=u'Client-Joe-    Mama', fields=[search.TextField(name=u'clientNumber', value=u'Client Joe Mama'), search.DateField(name=u'createDate', value=datetime.datetime(2015, 4, 23, 0, 0)), search.TextField(name=u'clientName', value=u'1'), search.TextField(name=u'address1', value=u'2'), search.TextField(name=u'address2', value=u'3'), search.TextField(name=u'phone', value=u'6'), search.TextField(name=u'city', value=u'4'), search.TextField(name=u'notes', value=u'9')], language=u'en', rank=135954464L), search.ScoredDocument(doc_id=u'405a779d-70e2-4ab8-a5d0-8dd989bcbc9a', fields=[search.TextField(name=u'number', value=u'Client Joe Mama'), search.DateField(name=u'createDate', value=datetime.datetime(2015, 4, 21, 0, 0)), search.TextField(name=u'name', value=u'1'), search.TextField(name=u'address1', value=u'2'), search.TextField(name=u'address2', value=u'3'), search.TextField(name=u'phone', value=u'6'), search.TextField(name=u'city', value=u'4'), search.TextField(name=u'notes', value=u'9')], language=u'en', rank=135843846L), search.ScoredDocument(doc_id=u'747703ec-ab4d-48e5-aef9-16b60f8129f7', fields=[search.TextField(name=u'number', value=u'Client Joe Mama'), search.DateField(name=u'createDate', value=datetime.datetime(2015, 4, 21, 0, 0)), search.TextField(name=u'name', value=u'1'), search.TextField(name=u'address1', value=u'2'), search.TextField(name=u'address2', value=u'3'), search.TextField(name=u'phone', value=u'6'), search.TextField(name=u'city', value=u'4'), search.TextField(name=u'notes', value=u'9')], language=u'en', rank=135843801L)], number_found=3L)
Sanders
  • 13
  • 4

1 Answers1

0

Use callback instead of return. Change your function to this:

function getSearchResults(searchFor, searchTerm, callback) //See callback
{
 var jsonData = JSON.stringify({
    searchFor: searchFor.value, 
    searchTerm: searchTerm.value, 
 });

 $.ajax({    
 url: '/json_client_search',
 type: 'POST',
 contentType: 'application/json',
 data: jsonData,
 success: function(response) {
    console.log(response);
    callback(response); //Using callback now
 },
 error : function(xhr,errmsg,err) {
    bootbox.alert(xhr.status);
  }
  });
}

And change your function call to

getSearchResults(searchFor, searchTerm, function(response){
   //Handle response here
   var searchResults = response;
   if (searchFor == "Client"){
        resetSearchClientTable();
        populateClientSearchTable(searchResults);
    }
    else if (searchFor == "Contact"){
        resetSearchContactTable();
        populateContactSearchTable(searchResults);
    }
});

You cannot do a return from async call, hence callback is the way.

Zee
  • 8,420
  • 5
  • 36
  • 58
  • That is helpful, but I am experiencing an issue now with the getSearchResults function erroring out on the server. Any thoughts? Is there possibly a syntax issue with what you have provided? – Sanders Apr 30 '15 at 10:40
  • @Sanders. Did you put the rest of the code from `if (searchFor == "Client"){..` inside `function(response){...` , as they are making use of `searchResults`.. I will update my answer. – Zee Apr 30 '15 at 10:44
  • @Sanders. If you have any syntax error, it shud be visible in the console – Zee Apr 30 '15 at 10:46
  • The problem is that it is sending a blank JSON object to the server using the method you provided. Thoughts? – Sanders Apr 30 '15 at 11:01
  • The `callback` shud not have any effect on what you are sending to server, try printing the `jsonData` to `console`, if it contains the correct values. And as `contentType` is `json`, I guess you don't have to stringify it, just send as it is. – Zee Apr 30 '15 at 11:04
  • Turns out I just needed to remove the .value from my original calls: var searchFor = document.getElementById("searchFor").value; var searchTerm = document.getElementById("searchTerm").value; – Sanders Apr 30 '15 at 11:14
  • It sure is. Thanks for all your help! Unfortunately, I can't upvote your answer, but please know that I really appreciate it! – Sanders Apr 30 '15 at 11:23