0

I would like to take the results of my $.getJSON and place that JSON data, specifically the facility. FacilityName, as the values for the array availableTags. I have tried executing a function call where you see the // TODO: but I am not getting this sorted out. I'm sure it is easy for your jQuery experts! I DO see the facilities displayed on my page in the <div id="info"></div> tag as I would expect.

<script type="text/javascript">

$(function () {
    $('#datePicker').datepicker();
});

var names = [];

$.getJSON('FacilitiesAsync','sourceDb=myDb',processFacilities);
function processFacilities(data) {
    $.each(data, function (facilities, facility) {
        names += facility.FacilityName;
    }); // end of each

    alert(names);

    $("#tags").autocomplete("source", names);
};

$(function () {
    $("#tags").focusout(function () {
        $.ajax({
            beforeSend: function() {
                alert("Testing");
            },
            complete: function () {
                alert("Done!");
            }
        });
    });
});

</script>
Brian Evans
  • 235
  • 1
  • 4
  • 21
  • *Inside* the getJSON callback: `$("#tags").autocomplete({ source: somehowUsingTheJSONResponseData });` (I believe `source` can also be a *function*.) – user2864740 Nov 19 '13 at 04:28
  • http://stackoverflow.com/questions/31129/how-can-i-return-a-variable-from-a-getjson-function/31153#31153 – user2864740 Nov 19 '13 at 04:29

1 Answers1

0

your TODO block contain inside of self invoking anonymous function, probably it executes before your getJSON executes, so in time of TODO block executing there is no json data. try to access data after getJSON function, may be in callback or within done of the getJSON.

Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51
  • Yes, I agree. HOW do I call the callback function to load the data for the autocomplete? So the page loads and this JSON data is returned in the background with an ajax call. Once I have all the data it will be available in the autocomplete input control – Brian Evans Nov 19 '13 at 04:35
  • Something happened to the post made by another user that was helping me – Brian Evans Nov 19 '13 at 05:04
  • I get the results I always had. I want to see them in the autocomplete. That is still not working. – Brian Evans Nov 19 '13 at 06:26
  • Are you using jQuery UI for autocomplete? – Ruwanka De Silva Nov 19 '13 at 06:44
  • But you didn't mention that in your question, that's why I ask, hence refer [link](http://jqueryui.com/autocomplete/) it seems your `autocomplete` have syntax error. And try with raw data for check whether it works – Ruwanka De Silva Nov 19 '13 at 07:07
  • I have done exactly that before I posted this question. It works with the raw data but I want to display my JSON data. That data looks like a comma separated list of strings without quotes. – Brian Evans Nov 19 '13 at 07:17
  • I have a valid json string returned from my service. I JUST want to take that string and make it the values that show up in autocomplete.....please – Brian Evans Nov 19 '13 at 07:24
  • ok, then are you sure about `names` contains array contains list of strings at `autocomplete` functoin calling? If it is, there should be another problem which caused to stop autocomplete functions execution properly. – Ruwanka De Silva Nov 19 '13 at 07:27