我正在尝试通过 jquery $.ajax();
提交一个 PHP 表单。它提交成功,但是当我尝试提醒消息时 - alert(SUCCESS);
成功。它不是。有什么猜测吗?
代码:
$.ajax({
url: 'basic_cms_manager_home_fb_form_submit.php',
type: 'POST',
data: formData,
cache: false,
dataType: 'json',
success: function(data, textStatus, jqXHR) {
if (typeof data.error === 'undefined') {
// Success so call function to process the form
alert("SUCCESS");
console.log('SUCCESS: ' + data.success);
} else {
// Handle errors here
console.log('ERRORS: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown) {
// Handle errors here
console.log('ERRORS: ' + textStatus);
},
complete: function() {
// STOP LOADING SPINNER
}
});
注意:我已经尝试过:console.log(data); n 其他技巧。我的问题是为什么 Alert 不工作,当整个脚本工作时以及为什么它给出 parseerror?
最佳答案
SUCCESS 不是一个变量而是一个字符串。你需要在它周围加上引号,比如 alert("SUCCESS");
.success
和 .error
方法的使用也已被弃用。使用 .done
和 .fail
或者您可以简单地执行以下操作
$.ajax({
url: 'basic_cms_manager_home_fb_form_submit.php',
type: 'POST',
data: formData,
cache: false,
dataType: 'json',
success: function(data)
{
alert("SUCCESS");
console.log('SUCCESS: ' + data.success);
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
console.log('ERRORS: ' + textStatus);
},
complete: function()
{
// STOP LOADING SPINNER
}
});
另一件事
Typeof null
返回一个对象,因此如果 data.errors
为 null,您的检查将失败。考虑做
if (!data.errors) {
...
}
如果你想坚持你的代码。
关于javascript - 如何 - 在成功提交表单后以 jquery ajax 形式发出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704743/