2011-11-07 111 views
0

我有这样一个功能:uiModal.simpleModal('<p>This is some content</p>');负载在通过HTML使用jQuery AJAX

这应该叫模态与传递的数据为模式的内容。例如(注意我没有添加modalHtml,因为它与这个问题无关)。

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() }); 
    }); 
}, 

但是它不会加载数据!有人能指出我在正确的方向

感谢

回答

1

看起来你还没有告诉你的Ajax方法从哪里加载数据。

你需要在URL中传递将从中加载数据:

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

UPDATE

下面给出你对此有何评论似乎什么AJAX是你可能有轻微的误解为...设计。

在你的情况下,你已经有了模态窗口的数据,所以你可以使用它而不需要ajax调用。

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

如果你还需要检查您respoonseHtml变量存在的数据,那么你可以做一个简单的if声明:

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'); 

} 
+0

没有URL,正在函数调用中传递的内容 – Cameron

+0

不是。 ajax方法用于从URL中获取数据。你这样做是错误的。忘掉ajax部分,在这种情况下你不需要它。 –

1

为什么我看不到任何URL的Ajax调用。 必须有一些你想要点击的网址来检索数据,如果没有,那你为什么要做$ .ajax?

+0

没有URL,正在传递的内容正在函数调用 – Cameron

+0

如果你传递的内容,为什么要使用AJAX? – Kyberias