我試圖不成功地將我的數組作爲其結果傳遞給外部函數?這裏是jsbin中的工作代碼:https://jsbin.com/kugesozawi/edit?js,console,output結果應該傳遞給returnSearch。如何從我的函數傳遞數組返回
var googleSuggest = function(returnSearch){
var term = $('#searchInput').val();
var result = [];
var service = {
youtube: { client: 'youtube', ds: 'yt' },
books: { client: 'books', ds: 'bo' },
products: { client: 'products-cc', ds: 'sh' },
news: { client: 'news-cc', ds: 'n' },
images: { client: 'img', ds: 'i' },
web: { client: 'hp', ds: '' },
recipes: { client: 'hp', ds: 'r' }
};
$.ajax({
url: 'https://clients1.google.com/complete/search',
dataType: 'jsonp',
data: {
q: term,
nolabels: 't',
client: service.web.client,
ds: service.web.ds
}
}).done(function(data) {
result.pop()
$.each(data[1], function(item, value) {
var stripedValue = value[0].replace(/<[^>]+>/g, '');
result.push(stripedValue);
})
console.log(result)
})
returnSearch = ['ActionScript', 'AppleScript', 'Asp']
return returnSearch
};
可能的重複[如何返回來自異步調用的響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-調用) – Frxstrem