【发布时间】:2016-10-31 02:26:00
【问题描述】:
我有一个简单的 AJAX 请求,它根据表格中的一些选择更新一些 PHP 会话变量。当他们做出选择时,它会触发一个 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 请求返回的错误字符串的消息。
这是我的脚本:
$(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 变量包含我想在警报中使用的字符串。是否有可能以某种方式使用它并将其插入显示的警报中?
【问题讨论】:
标签: javascript php jquery ajax twitter-bootstrap