0

I have a function like: uiModal.simpleModal('<p>This is some content</p>');

Which should call a modal with the passed data as the modal content. e.g. (note I haven't added the modalHtml as it's not relevant to this question).

simpleModal: function (data) {

    var responseHtml = data;

    // Append the modal HTML to the DOM
    var modalInstance = $('body').append(modalHtml);

    // Dynamically load in the passed data from the call
    $.ajax({
        timeout: 5000,
        success: function (responseHtml) {
            $(modalInstance).find('.uiModalContent').html($(responseHtml));
            $(modalInstance).find('.uiModalContent').removeClass('loading');

            isModalSuccess = true;
        },
        error: function () {
            $(modalInstance).find('.uiModalContent').html('<div class="message error"><h2>Unknown Error</h2> Please contact support</p></div>');
            $(modalInstance).find('.uiModalContent').removeClass('loading');
        }
    });

    $(modalInstance).find('.ModalClose').live('click', function (e) {
        e.preventDefault();
        $(this).parents('.uiModal').fadeOut(function () { $(this).parents('.uiModalWrapper').remove() });
    });
},

However it doesn't load in the data! Can someone point me in the right direction

Thanks

2 Answers 2

1

It looks like you haven't told your ajax method where to load the data from.

You need to pass in a URL from which you will load your data:

$.ajax({
        url: "http://www.example.com/jsonRequest",
        timeout: 5000,
        success: function (responseHtml) {}
});

UPDATE

Given your comment below it seem that you might have a slight misunderstanding of what ajax is designed for.

In your case you already have the data for the modal window and so you can use it without the need for an ajax call.

    $(modalInstance).find('.uiModalContent').html($(responseHtml));
    $(modalInstance).find('.uiModalContent').removeClass('loading');

If you also need to check that the data exists in your respoonseHtml variable then you can do that with a simply if statement:

if(responseHtml.length > 0)
{
        $(modalInstance).find('.uiModalContent').html($(responseHtml));
        $(modalInstance).find('.uiModalContent').removeClass('loading');
}
else
{
   $(modalInstance).find('.uiModalContent')
     .html('Error message here');
   $(modalInstance).find('.uiModalContent').removeClass('loading');

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

2 Comments

There is no URL, the content is being passed in the function call
It isn't. The ajax method is for retriving data from a URL. You're doing this the wrong way. Forget about the ajax part, you don't need it in this case.
1

Why I do not see any URL for the ajax call. There must be some url you want to hit, to retrieve data, if not then why you are doing $.ajax ?

2 Comments

There is no URL, the content is being passed in the function call
If your passing the content, why use AJAX?

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.