jQuery中的Ajax调用-未捕获的TypeError无法读取null的属性“错误" [英] Ajax call in jQuery - uncaught typeerror cannot read property 'error' of null
本文介绍了jQuery中的Ajax调用-未捕获的TypeError无法读取null的属性“错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用下面的jQuery进行ajax调用.
I am trying to make an ajax call using the below jQuery.
但是我可以在Chrome中看到,我遇到了无法捕获的typeerror,无法读取null的属性"error".因此,这将停止添加类(成功").知道为什么要这么做吗?我是否需要以某种方式声明错误"?
But I can see in Chrome, i'm getting uncaught typeerror cannot read property 'error' of null. So this stops the class ('success') getting added. Any idea why it's doing this? Do I somehow need to declare 'error'??
function submitForm(formData) {
$.ajax({
type: 'POST',
url: 'mailer.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
$("form #form_ajax").removeClass().addClass((data.error === true) ? "error" : "success")
.html(data.msg).fadeIn('fast');
if ($('form #form_ajax').hasClass('success')) {
setTimeout("$('form #form_ajax').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('form #form_ajax').removeClass().addClass('error')
.html('<p>There was an ' + errorThrown + 'error due to a ' + textStatus + ' condition.</p>').fadeIn('fast');
},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};
谢谢...
推荐答案
将代码编辑为:
success: function(data) {
if ( data.error) alert(data.error);
alert(data);
$("form #form_ajax").rem...
并告诉我警报msg(第2个警报)
and tell me the alert msg (theres 2)
没有任何意义:
执行以下操作:
这篇关于jQuery中的Ajax调用-未捕获的TypeError无法读取null的属性“错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文