如何检索jQuery从servlet发送的JSON数组

时间:2021-01-27 13:19:44

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

我正在尝试检索数据库中的所有图像并在网页中显示。servlet发送一个项目数组。当我运行我的servlet程序时,它会显示出来

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

But in web page the jQuery returns undefined.

但是在web页面中,jQuery返回未定义的。

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

我是jQuery新手,请大家帮我解决这个问题。

Javascript :

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 个解决方案

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

&确保您正在发送一个标题('Content-Type: application/json');在回应你的回应。

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

您还需要移动$('ul.es-carousel').追加(items.join("));每美元

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

&确保您正在发送一个标题('Content-Type: application/json');在回应你的回应。

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

您还需要移动$('ul.es-carousel').追加(items.join("));每美元