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)