0

I am having a little bit of trouble here. I have a call that returns some JSON, that is well-formed. I checked it on JSON Lint.

JSON: {"facilities":[{"facilityId":"123","facilityName":"Pizza Hut"}]}

I am able to see that data in my alert box with this code:

 $(function () {
    var availableTags = [
        // TODO
    ];
    $("#tags").autocomplete({
        source: availableTags
    });
    $("#tags").focusout(function () {
        var result = null;
        $.ajax({
            beforeSend: function() {
                alert("Testing");
            },
            url: "FacilitiesAsync",
            success: function(data) {
                result = data;
            },
            complete: function () {
                alert(result);
            }
        });
    });
});

However, when I try this code I don't ever see the processFacilities function ever fire. I am not sure what I am doing incorrectly. The $getJSON DOES work but the callback function is never executed.

   $.getJSON('FacilitiesAsync','sourceDb=sampleDb',processFacilities);
    function processFacilities(data) {
    var infoHtml = '';
    $.each(data, function(facilities, facilityInfo) {
        infoHtml += '<p>Facility: ' + facilityInfo + '<br></p>';
    }); // end of each

    $('#info').html(infoHtml);
};

I am not trying to run both of these at the same time. I commented one out while I test the other. These scripts are within a block in my MVC4 application.

My div

<div id="info"></div>
2
  • How do you KNOW that it works? It seems it doesn't, or else that call would happen. What does you Network tab say? Commented Nov 15, 2013 at 17:41
  • I know the JSON is returned. I have stepped through the code. What I am seeing now is "Facility: undefined" Commented Nov 15, 2013 at 17:50

2 Answers 2

1

You miss the facilityName inside the function. Try this:

   $.getJSON('FacilitiesAsync','sourceDb=IPACS',processFacilities);
    function processFacilities(data) {
    var infoHtml = '';
    $.each(data, function(facilities, facilityInfo) {
        infoHtml += '<p>Facility: ' + facilityInfo.facilityName + '<br></p>';
    }); // end of each

    $('#info').html(infoHtml);
};
Sign up to request clarification or add additional context in comments.

8 Comments

I tried adding the facilityName property to the facilityInfo object and I still don't see anything in the UI. I also added above my <div> where this should be shown.
Yea, I am not working on the autocomplete just yet. That is the goal eventually.
No, I don't know what that is seattle
I am getting "Facility: undefined" rendered in the DIV when I add the facilityName to the facilityInfo object.
Yes, I have Skype but it is on my Mac, ugh
|
0

i think a mix of both will do it proper

:

        $("#tags").focusout(function () {
        var result = null;
        $.ajax({
            beforeSend: function() {
                alert("Testing");
            },
            url: "FacilitiesAsync",
            success: function(data) {
                  var infoHtml = '';
                  $.each(data, function(facilities, facilityInfo) {
                       infoHtml += '<p>Facility: ' + facilityInfo + '<br></p>';
                  }); // end of each

                  $('#info').html(infoHtml);
            },
            complete: function () {

            }
        });
    });
});

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.