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

时间:2015-04-30 10:14:32

标签: javascript javascript-objects

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

这是调用函数:

//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);
        }
    }
}

这是JSON函数:

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)

1 个答案:

答案 0 :(得分:0)

使用callback代替return。将您的功能更改为:

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);
    }
});

您无法通过return来电async,因此callback就是这样。