如何在jquery中附加带有div标签的html响应(来自ajax请求)?

M S*_*ach 2 ajax jquery jquery-ui

下面是代码片段,它正在命中服务器上的url并获取html响应.我可以在firefox调试器中看到响应,但它不会显示在div标签中.

$.ajax({
    url: url,
    dataType: 'html',
    data: '',
    type: 'POST',
    success: function(data) {
        //in firefox debugger i can see complete html response inside data 
        $('#displayContent').html(data); // but here, it does not  
        // append the html inside div displayContent. Instead it makes 
        // the current page blank
    }
});?
Run Code Online (Sandbox Code Playgroud)

我没有得到我在这里犯的错误我可以不直接将ajax html响应分配给选择器(在我的情况下为div标签)$('#displayContent').html(data)吗?

Kun*_*han 5

而不是使用html()方法使用append ie

$('#displayContent').append(data);
Run Code Online (Sandbox Code Playgroud)

或者,如果要将整个内容直接分配给元素,请使用load方法

$(function(){
   $('#displayContent').load(url);
});
Run Code Online (Sandbox Code Playgroud)