2

I'm trying to retrieve all images in database and display in webpage. The servlet sends an array of items. When I run my servlet program it displays

{"success":true,"imageInfo":[]}

But in web page the jQuery returns undefined.

I'm newbie in jQuery - please anyone help me solve this.

Javascript :

$(window).load(function() 
            {
            $.ajax({
               type: "GET",
                url: "RetriveIm",
                dataType: "json",
                success: function( data, textStatus, jqXHR) 
                {
                    if(data.success)  
                      {

                        alert(console.log(data.imageInfo.name));
                        var items = [];
                        $.each(data, function(i, item) 
                        {
                          items.push('<li><a href="#"><img src=data:image/png;base64,'+ item.thumbarray +' data-large=data:image/png;base64,' + item.imageInfo.fullarray + ' data-description=' + item.imageInfo.disc + ' alt=' + item.imageInfo.name + ' data-id='+ item.imageInfo.imageid +'/></a></li>');  // close each()
                          $('ul.es-carousel').append( items.join('') );
                         });
                       };
                  },
             error: function(jqXHR, textStatus, errorThrown)
              {
                 alert("error");
                 console.log("Something really bad happened " + textStatus);
              },
              });       
             });

My server side program is in this question.

I don't what error in data please help me.....Thanks...

Community
  • 1
  • 1

1 Answers1

1
$.getJSON("url",function(json){
         console.log(json)
         //your set of images check object structure
         $.each(json.imageInfo, function(i, item) {//... if i understood well the json you are sending
         });

shall be enough for your needs.

& make sure you are sending an header('Content-Type: application/json'); before echoing your response.

you also need to move $('ul.es-carousel').append( items.join('') ); out of the $.each

mikakun
  • 2,203
  • 2
  • 16
  • 24
  • put the correct url & look into the console for error message or the log of the json; if nothing look into the response sent by "url". You know how to look into console right ? – mikakun Feb 09 '13 at 10:24
  • in the console upon the ajax request ? then it means the "url" you have provided in the getJSON is not found – mikakun Feb 09 '13 at 10:38
  • I'm providing right url. this was servlet mapping ` RetriveIm RetriveIm skypark.RetriveIm RetriveIm /sp/RetriveIm` I'm providing `url="RetriveIm"` Now tell me what was the error. This was killing me from two days...??????????????? –  Feb 09 '13 at 10:50
  • type RetriveIm in your browser (next to the last slash of your page url - in place of page script name) & see what you get -> if 404 not found; you need to provide a valid url pointing to the server script, find it ! – mikakun Feb 09 '13 at 11:12
  • Sorry sir it was producing `HTTP Status 500 - Servlet execution threw an exception`. I don't how it happened just before it was working correctly....!!!!!! –  Feb 09 '13 at 11:23
  • maybe it's .htaccess config problem maybe it's expecting to receive some get or post data along the http request... it's hard to know from here – mikakun Feb 09 '13 at 11:27
  • The server side problem was resolved but the problem specified in this question was remain same.The `console.log(json)` showing `undefined` what this means.....???? –  Feb 09 '13 at 13:44
  • check the ajax response from the console; maybe could mean the response is not sent with the correct header – mikakun Feb 09 '13 at 13:48
  • In firebug the response received from the server is `{"success":true,"imageInfo":[]}` is this means imageInfo is `null`.. –  Feb 09 '13 at 14:20
  • yeah most probably; look in header of response also to check content type is set as application/json because even if imageInfo is empty json should be defined – mikakun Feb 09 '13 at 14:24
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24236/discussion-between-james-and-mikakun) –  Feb 09 '13 at 14:28