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...

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

11 Comments

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 ?
in the console upon the ajax request ? then it means the "url" you have provided in the getJSON is not found
I'm providing right url. this was servlet mapping <servlet id="spR"> <display-name>RetriveIm</display-name> <servlet-name>RetriveIm</servlet-name> <servlet-class>skypark.RetriveIm</servlet-class> </servlet> <servlet-mapping id="spRm"> <servlet-name>RetriveIm</servlet-name> <url-pattern>/sp/RetriveIm</url-pattern> I'm providing url="RetriveIm" Now tell me what was the error. This was killing me from two days...???????????????
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 !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.