0

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() ???

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

You should JSON encode your array at the server side which will transfer it back as JSON object and mention dataType :'JSON' in your ajax call for this purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.