我有下面的代码,这是通过AJAX和JSON的表单提交,但我在Chrome浏览器中收到uncaught type error cannot read property 'error' of null消息时,我查看脚本。
我完全是JSON的新手,所以也许我宣布了一些错误?
$(document).ready(function(){
$('form #form_ajax').hide();
$('#submit').click(function(e){
e.preventDefault();
var valid = '';
var required = ' is required';
var name = $('form #name').val();
var email = $('form #email').val();
var message = $('form #message').val();
if (name == '' || name.length <= 2) {
valid = '
Your name' + required + '
';}
if (!email.match(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i)) {
valid += '
Your email' + required + '
';}
if (message == '' || message.length <= 5) {
valid += '
A message' + required + '
';}
if (valid != '') {
$('form #form_ajax').removeClass().addClass('error').html('
Please correct the errors below.
' + valid).fadeIn('fast');} else {
$('form #form_ajax').removeClass().addClass('processing').html('Processing…').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
});
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")
/HERE'S WHERE I GET THE UNCAUGHT EXCEPTION ERROR
.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('
There was an ' + errorThrown + 'error due to a ' + textStatus + ' condition.
').fadeIn('fast');},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};
+0
这里有一些代码丢失吗? –
+0
我和@DaveNewton一样迷失。你真的在哪里做Ajax请求? –
+0
实际上从服务中返回的数据是什么? –