views:

160

answers:

3

Hi All,

I am using the following code to get data from the database( from cs page itself i am creating the html code) and binding the html code to the div.

Problem:

If the database size is higher it takes some time to show the result. thet time i want to shoe a loading.gif image in that location. Once it get the data i have to hide the load image.

Edit:

Problem: Once it get hide, then the show() is not working.

 <div id="searchContainer" class="search_outer">
        <div id="Loading"></div></div>

Code:

    $.ajax({
                  type: "POST",
                  contentType: "application/json; charset=utf-8",
                  data: "{ searchText: '" + searchText + "', product: '" + product + "', category: '" + category + "', artist:'" + artist + "'}",
                  url: "Search.aspx/FetchSearchResult",
                  dataType: "json",
                  success: function(data) {  $("#Loading").hide(); $("#searchContainer").html(data.d[0]);}});


     $("#ajax-query-place").ajaxStart(function() {
                      $("#Loading").show();
                  });

Geetha.

+1  A: 

Easy: Before launching the ajax()-methode, display the spinner image. In the success method, hide it again.

christian studer
+1  A: 
$.ajax({
                  type: "POST",
                  contentType: "application/json; charset=utf-8",
                  data: "{ searchText: '" + searchText + "', product: '" + product + "', 
                  category: '" + category + "', artist:'" + artist + "'}",
                  url: "Search.aspx/FetchSearchResult",
                  dataType: "json",
                  success: function(data) { $("#searchContainer").html(data.d[0]);
                                            $("#loading-image").hide();
}});

$("#ajax-query-place").ajaxStart(function(){
      $("#loading-image").show();
});

$("#ajax-query-place") is an element which is getting or sending ajax queries.

sundowatch
I don't understand this answer. `ajaxQuery` contains an `XMLHTTPRequest` object. So what does `ajaxQuery.ajaxStart(...)` do? Did you test this?
patrick dw
ajaxQuery is a variable. ajaxQuery.ajaxStart(function(){}); returns a function when ajax started. Yes I tried.
sundowatch
Showing error: "Microsoft JScript runtime error: Object doesn't support this property or method"
Geetha
try on Firefox, please.
sundowatch
sorry, i have to work on ie8.
Geetha
@sundowatch - ajaxQuery is a variable that stores an `XMLHTTPRequest` object, which does not have a `ajaxStart()` function. This does not work. The `$.ajax()` request may work, but your `ajaxQuery.ajaxStart(...)` call will fail. The correct form for `ajaxStart()` is `$("#loading-image").ajaxStart(function() {...});`.
patrick dw
@Geetha - You're getting that error because sundowatch is trying to run `startAjax()` against an object that does not have that method. See my comment above.
patrick dw
Yeah the upper comment is true. I forgot. I edited my answer.
sundowatch
For the first click it is working fine. But for the next search the loading image is not showing. I have edited the question with the html code.
Geetha
+1  A: 

The trouble is in your success: callback. When you do:

$("#searchContainer").html(data.d[0]);

You are overwriting the #Loading element because it is inside #searchContainer.

Use append() instead.

function(data) {  
    $("#Loading").hide(); 
    $("#searchContainer").append(data.d[0]);
}

Or just move the #Loading element outside of the #searchContainer element.


EDIT:

I'm guessing you don't have an element called #ajax-query-place.

You should use:

$("#searchContainer").ajaxStart(function() {
     $("#Loading").show();
});
patrick dw
After calling hide() show is not working.(Control is not moving to $("#ajax-query-place").ajaxStart(function() :(
Geetha
On page load i am hiding the loading image. after that it is not showing.
Geetha
I assume you tried one of my solutions above. If so, place an alert in your `ajaxStart()` function, like `alert('ajaxStart was called');`. If `ajaxStart()` is getting called, and it is still not showing, we know there's another issue.
patrick dw
It is not showing the message inside ajaxStart().
Geetha
I just updated my answer. I'm guessing you don't have an element called `#ajax-query-place`, but rather you just used that from another answer here. That was meant to be a place holder since you didn't have any code at the time.
patrick dw