0

I am trying to compare 2 arrays. One is taken from DOM, generated by .split(" "), the other I would like to be retrived by $.get call.

All is working fine if I test the dictionary array manually. Example:

  // var dictionary = ["in", "the"]; // <- this one works
  var dictionary = $.getJSON('job/dictionary/en');
  $(this).keyup(function() {
      var words = $(this)
                    .text()
                    .split(" "),
          words_final = [],
          i = 0;
        jQuery.grep(words, function(word) {
            if (jQuery.inArray(word, dictionary) == -1) words_final.push(word);
            i++;
        });
    });

Response from this URL is Content-Type: application/json with response body:

[
   "in",
   "the"
]

Apparently... then I try to run $.getJSON('job/dictionary/en') in chrome dev tools, I get an object back with responseText: "["in","the"]"

I've tried with $.get() as well with the same result.

Any help really appreciated.

Jacek Dominiak
  • 857
  • 1
  • 9
  • 19
  • 3
    Welcome to the wonderful world of **async**! You can't do that. – SLaks Mar 17 '13 at 16:17
  • You must defer using the response from the JSON call until the call has actually completed; the JavaScript program doesn't wait for the response. Use [`Deferred`s](http://api.jquery.com/jQuery.Deferred) or the [`success`](http://api.jquery.com/jQuery.getJSON) handler, and put your code in there instead. – Matt Mar 17 '13 at 16:19
  • @dystroy, I'd like to close this as a duplicate but I consider that question you want to close it against is badly formatted and explained. I'll try to look for a better one among the thousand of possibilities – Alexander Mar 17 '13 at 16:37
  • @Bergi, perfect indeed. I mark the question as solved. – Jacek Dominiak Mar 17 '13 at 16:55

0 Answers0