将对象从一个Javascript函数返回到另一个Javascript函数

将对象从一个Javascript函数返回到另一个Javascript函数,javascript,javascript-objects,Javascript,Javascript Objects,这是它的长短。我试图从下拉列表和文本框中获取数据,通过JSONAJAX发送数据,以查询搜索索引并返回类似字典的对象。该对象正在从服务器返回,但我不确定如何将其返回到调用它的函数 以下是调用函数: //Checks search form to ensure fields are populated. If populated calls the appropriate function to search either Contact or Client index. //Receives r

这是它的长短。我试图从下拉列表和文本框中获取数据,通过JSONAJAX发送数据,以查询搜索索引并返回类似字典的对象。该对象正在从服务器返回,但我不确定如何将其返回到调用它的函数

以下是调用函数:

//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);
        }
    }
}
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);
    }
    });
}
以下是JSON函数:

//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);
        }
    }
}
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);
    }
    });
}
以下是通过console.log返回的结果,但不会返回到原始调用函数。当我在那里记录它时,它是未定义的

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)

使用
回调
而不是
返回
。将您的功能更改为:

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);
  }
  });
}
并将函数调用更改为

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

您无法从
async
调用执行
return
,因此
callback
是一种方法。

这很有用,但我现在遇到了一个问题,服务器上的getSearchResults函数出错。有什么想法吗?您提供的内容可能存在语法问题吗?@Sanders。您是否将
if(searchFor==“Client”){..
中的其余代码放在
函数(response)中{…
,因为他们正在使用
搜索结果
。我将更新我的答案。@Sanders。如果您有任何语法错误,它应该在控制台中可见。问题是它正在使用您提供的方法向服务器发送一个空的JSON对象。想法?
回调
对您发送的内容没有任何影响对于服务器,如果
jsonData
包含正确的值,请尝试将
jsonData
打印到
console
。由于
contentType
json
,我想您不必将其字符串化,只需按原样发送即可。@James Thorpe同意,感谢您为我指明本文的方向。