2016-09-07 46 views
0

我用下面的代码显示一个div通知的问题:显示DIV失败AJAX请求(DIV总是隐藏)

$.ajax({  
    url: url, 
    cache: false, 
    async: false, 
    success: function (data) 
    { 
     response = jQuery.parseJSON(data); 
    }, 
    error: function(){ 
     showAlert('contentContainer', { type: 'danger', title: 'Error', text: 'Connection Failed' }); 
    } 
}); 

当服务器发生故障的股利应显示。但div总是被隐藏。在没有网络错误发生的其他地方使用showAlert()方法工作正常。

function showAlert(parentDiv, options) { 
    $alertDiv = $('<div></div>') 
     .show() 
     .prop({ id : 'alert'}) 
     .addClass('alert-' + options.type) 
     .append($('<button></button') 
      .attr({ type: 'button'}) 
      .addClass('close') 
      .data('dismiss', 'alert') 
      .html('X') 
      .on('click', function() { 
       $(this).parent().remove(); 
      }) 
     ); 

    if("title" in options) { 
     $alertDiv.append($('<h4></h4>') 
      .html(options.title) 
     ); 
    } 

    $alertDiv.append($('<p></p>') 
     .html(options.text) 
    ); 

    $('#' + parentDiv).prepend($alertDiv); 
} 

浏览器控制台:

Failed to load resource      http://192.168.182.130:8080/js/jquery-3.1.0.min.js 
    send          jquery-3.1.0.min.js:4 
    r.extend.ajax        jquery-3.1.0.min.js:4 
    getFromDatabase       utils.js:251 
    (anonymous function)      (index):39 
    r.event.dispatch       jquery-3.1.0.min.js:3 
    q.handle 

任何想法发生了什么吗?

在此先感谢!

+0

'$('#'+ parentDiv).prepend($ alertDiv);'...在这种情况下什么是'parentDiv'?你有没有尝试在失败的AJAX调用后检查DOM,看看你是否可以在某处找到你的div? –

+0

@TimBiegeleisen div正在被插入。这只是'style ='display:none''而不是'block'。这只发生在网络错误之后。插入div工作正常否则。 – boindil

回答

0

嗯...我找到了解决我的问题。

但是我不知道为什么会发生这种情况。

$alertDiv = $('<div style="display: block !important"></div>') 

上面的代码适用于我。谢谢你的帮助!