I have this piece of code:
var suggest=$.ajax({
cache: true,
type: 'GET',
url: solrServer + "suggest?q=" + valore + ec + "wt=json&omitHeader=true&json.wrf=?",
dataType: "jsonp",
success: function (data) {
data = parse();
function parse() {
var parsedQueries = [];
for (var i = 0; i < data.spellcheck.suggestions[1].suggestion.length; i++) {
console.log('i_esimo: ' + data.spellcheck.suggestions[1].suggestion[i]);
parsedQueries[i] = data.spellcheck.suggestions[1].suggestion[i];
}
return parsedQueries;
}
}
});
console.log('suggest: ' + suggest);
when i print in console:
console.log('i_esimo: ' + data.spellcheck.suggestions[1].suggestion[i]);
I visualize all element of response and after i assign it at array parsedQueries, finally return parsedQueries, that should be assigned to my var suggest, but when i print in console suggest, i have:
suggest: [object Object]
and not my array of value. The question is: how do I return an array of values (string) from 'success' of jQuery.ajax() ???
Since ajax is executed asynchronously it is not possible to return a value from ajax request.
One possible solution is to make the request synchronous using the async: false
flag, but it is not recommended.
Another solution is to use a callback method, to handle the result of the ajax request
您应该在服务器端JSON encode
数组进行JSON encode
,然后将其作为JSON
对象传输回去,并为此在ajax调用中提及dataType :'JSON'
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.