2016-10-31 13 views
0

テーブル内のいくつかの選択に基づいてPHPセッション変数を更新する簡単なAJAXリクエストがあります。 AJAXリクエストを選択すると、AJAXリクエストが発生します。現時点では私はAJAXからのブートストラップアラートテキストの更新リクエスト結果

<div id="alert_ajax_error" class="alert alert-danger text-center" role="alert" style="display:none"> 
 
    There was an error updating your selections - please contact the Support Desk 
 
</div>

隠されたブートストラップの警告を示すことにより、一般的なエラーメッセージを表示していますAJAX要求に誤りがあります場合、私は今、そこ(一般的なエラーメッセージを変更したいですAJAXリクエストから返されたエラー文字列を表示するものに、あなたの選択を更新するエラーでした - サポートデスクに連絡してください)。ここで

は私のスクリプトです:

$(document).ready(function() { 
 
    $('button.btn-success').click(function() { 
 
    var itemID = $(this).closest('tr').attr('id'); 
 
    console.log(itemID); 
 
    // Create a reference to $(this) here: 
 
    $this = $(this); 
 
    $.post('updateSelections.php', { 
 
     itemID: itemID, 
 
     selectionType: 'yes' 
 
    }, function(data) { 
 
     data = JSON.parse(data); 
 
     if (data.error) { 
 
     var ajaxError = (data.text); 
 
     console.log(ajaxError); 
 
     console.log('something was wrong with the ajax request'); 
 
     $this.closest('tr').addClass("warning"); 
 
     //make alert visible 
 
     $("#alert_ajax_error").show(); 
 
     return; // stop executing this function any further 
 
     } else { 
 
     console.log('update successful - success add class to table row'); 
 
     $this.closest('tr').addClass("success"); 
 
     $this.closest('tr').removeClass("danger"); 
 
     //$(this).closest('tr').attr('class','success'); 
 
     } 
 
    }).fail(function(xhr) { 
 
     console.log('ajax request failed'); 
 
     // no data available in this context 
 
     $this.closest('tr').addClass("warning"); 
 
     //make alert visible 
 
     $("#alert_ajax_error").show(); 
 
    }); 
 
    }); 
 
});

ajaxError変数は、私は私の警告に使用したい文字列が含まれています。何とかこれを使用して表示される警告に挿入することは可能ですか?

答えて

1

あなたは経由してあなたの応答を挿入することができます。

$('#alert_ajax_error').html(data); 
関連する問題