0

I am new to using the jQuery JSON request method, and I have a feeling I am making a really silly mistake with it.

For some reason, the following is not being called. The alert box never opens, and the html variable never is populated.

Any Ideas?

$.getJSON('data.json', function(data){
                json = data;

                html = '<div class=' + id + '><div id=' + id + ' class="child" style="display:none;">';
                for (x in json[elementId].images) {
                    html += '<img src=\"' + json[elementId].images[x] + '\" />';
                }
                html += '</div></div>';
                alert(html);
            });

Thanks a lot,

Isaac

4
  • what's id? where is it being populated? Commented May 27, 2011 at 15:52
  • id is a parameter passed to the parent function in which this $.getJSON lives. Commented May 27, 2011 at 16:27
  • is this served from a web server or file:/// ? Commented May 27, 2011 at 16:44
  • Web server... file:/// gets weird cross domain errors. Commented May 27, 2011 at 17:11

1 Answer 1

1

Have you checked your Net tab in Firebug to see if the request is actually successful? You might be getting some sort of an error (connection failed or improperly-formatted JSON perhaps). Try converting the jQuery.getJSON into a jQuery.ajax with an error handler to see if you have errors:

$.ajax({
  url: 'data.json', 
  dataType: "json",
  success: function(data, textStatus, jqXHR) {
    json = data;

    html = '<div class=' + id + '><div id=' + id + ' class="child" style="display:none;">';
    for (x in json[elementId].images) {
       html += '<img src=\"' + json[elementId].images[x] + '\" />';
    }
    html += '</div></div>';
    alert(html);
  },
  error: function(jqXHR, textStatus, errorThrown) {
     console.log(jqXHR, textStatus, errorThrown);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try it out and let you know

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.