我在页面上有一个表单,它通过 jQuery 将新数据添加到数据库中。它工作正常,但有时服务器会崩溃,我得到一个500 (Internal Server Error),但那是因为我的服务器很糟糕。
问题是我有时会在 PHP 向数据库添加内容后收到服务器错误,但我没有收到成功消息,而是收到错误消息,即使添加已经完成。那么我应该怎么做才能确保如果我收到错误 PHP 不会添加数据呢?
[是的,我最终会切换到新服务器,但没有服务器是完美的]
这是脚本:
$.ajax({
type: "POST",
url: "/clase/do-add",
data: $("#add").serialize(),
dataType: "json",
timeout: 8000,
beforeSend: function() {
var icon = '<p class="loading-add"></p>'; // loading icon
$('form#add').append(icon);
},
success: function(data) {
if (data.error == true) { // php returns error = true if name is invalid
alert('Invalid name.');
$('form#add').find('p').remove();
} else {
// make the addition
}
},
error: function () {
$('form#add').find('p').remove();
alert('Error. Try again.');
}
});