JQuery for loop after ajax call

advertisements

I am trying to loop the data returned after ajax call is success and adding the result to pagination plugin.

Here is my code:

 var imagesPerPage = 2, pageNumber = 1;
  var pagesContainer = $('#pagesContainer'),
  imagesInPage = 0,
  divPage = $("#p1");

    $.ajax({
           type: "POST",
           url: "Default.aspx/GetImages",
           data:{},
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (response) {

             //Here goes my for loop                    

           }
      });

This is my for loop where I need to get the images from webmethod and loop them and add to the pagination.

 for (i = 0; i < response.d.length; i++) {
          if (imagesInPage >= imagesPerPage) {
          imagesInPage = 1;
          pageNumber += 1;
          divPage = $('<div/>', { id: "p" + pageNumber }).addClass('pagedemo').hide().appendTo(pagesContainer);
          } else {
          imagesInPage += 1;
          }
}

Here is my pagination default configuration:

 $("#pagination").paginate({
              count: pageNumber,
              start: 1,
              display: Math.min(7, pageNumber),
              border: true,
              border_color: '#fff',
              text_color: '#fff',
              background_color: 'black',
              border_hover_color: '#ccc',
              text_hover_color: '#000',
              background_hover_color: '#fff',
              images: false,
              mouse: 'press',
              onChange: function (page) {
              $('#paginationdemo ._current').removeClass('_current').hide();
              $('#p' + page).addClass('_current').show();
        }
  }); //pagination

Now my problem is when I try to get the response of one Image it is not showing me the image as well as the pagination.If it is more than one then it starts showing me the pagination and image.

Screenshot:

So can anyone say me where am I going wrong?


where are you trying to get the response for one image? If you are trying to do this in your if condition then it will not work for only one response because

imagesInPage >= imagesPerPage

evaluates to false as imagesPerPage = 2 and imagesInPage=0;

also if your response type is json and this does not solve your problem try using

$.each(response,function(index,data){
    //check each image here
      // data.id gives you the id of each json object in the response
})