0

I have tried this code locally and I can't seem to get it to display the contents of the html file in my country_slide div. I've tried playing around with it a bit all to no avail. I can't seem to see any code to actually tell the ajax where to put the content.

http://jsfiddle.net/spadez/uhEgG/23/

window.onload = function () {
    var a = document.getElementById("country_link");
    a.onclick = function () {
        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/ajax/test.html',
            timeout: 5000,
            success: function (data, textStatus) {
                $("#country_slide").show();
                alert('request successful');
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            }
        });
        return false;
    }
}
4
  • There are two questions here it seems, which is it? Are you trying to display a loading variable or trying to display the result data when the request completes? Commented Jun 5, 2013 at 23:23
  • You're mixing and matching JQuery, which is not invalid, but it is odd. Are you sure that there are no Javascript errors occuring? Commented Jun 5, 2013 at 23:23
  • Sorry I have clarified my question now Commented Jun 5, 2013 at 23:23
  • In your JSFiddle example, div#country_slide has no content, and therefore show will display nothing. Not to mention that it is visible by default. You must be looking for something along the lines of TGH's answer. Commented Jun 5, 2013 at 23:25

2 Answers 2

4

I usually insert the content into the parent element using the .html function

window.onload = function () {
    var a = document.getElementById("country_link");
    a.onclick = function () {
        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/ajax/test.html',
            timeout: 5000,
            success: function (data, textStatus) {
                $("#country_slide").html(data);
                alert('request successful');
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            }
        });
        return false;
    }
}

This way the spinner can initially be placed inside the parent, and then be overwritten when the content is returned from the server

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

Comments

1

You can use beforeSend and complete methods to show or hide the loading symbol

beforeSend : function() {
   $('.loader').show();
}, 
success: function (data, textStatus) {
    $("#country_slide").show();
    alert('request successful');
},
error: function (xhr, textStatus, errorThrown) {
    alert('request failed');
},
complete : function() {
   $('.loader').hide();
}, 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.