我是 jquery 的新手,但仍然对其概念感到困惑。
我有一个用于注册新成员的 php MySQL 脚本。我刚刚介绍了一个 AJAX 脚本,它可以让我上传完成的表单而无需重新加载页面。
我在下面附上了我的代码。问题是我不断收到这条消息:
**> There was an SyntaxError: JSON.parse: unexpected character error due
> to a parsererror condition.**
有人可以告诉我这是什么意思吗,我查看了我的代码,但似乎找不到任何问题。
$('form #response').hide();
$('#submit').click(function(e) {
// prevent forms default action until
// error check has been performed
e.preventDefault();
// grab form field values
var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var cat = $('form #cat option:selected').val();
var location = $('form #country_loc option:selected').val();
var honeypot = $('form #honeypot').val();
var humancheck = $('form #humancheck').val();
// perform error checking
if (name = '' || name.length <= 2 || name == 'name*' ) {
valid = '<p>Your name' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Your email' + required +'</p>';
}
if (cat = '' || cat.length < 1) {
valid += '<p>Please tell us whether you are; seeking work or looking for a careworker' + required + '</p>';
}
if (location = '' || location.length < 1) {
valid += '<p>Please tell us your current location' + required + '</p>';
}
if (honeypot != 'http://') {
valid += '<p>Spambots are not allowed.</p>';
}
if (humancheck != '') {
valid += '<p>A human user' + required + '</p>';
}
// let the user know if there are erros with the form
if (valid != '') {
$('form #response').removeClass().addClass('error')
.html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast')
.html('<strong>the category is '+ cat + '.</strong>' +valid).fadeIn('fast');
}
// let the user know something is happening behind the scenes
// serialize the form data and send to our ajax function
else {
$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
//END THE FUNCTION FOR THE SUBMISSION OF REGISTATION FORM TO THE DATABASE VIA AJAX
// make our ajax request to the server
function submitForm(formData) {
$.ajax({
type: 'POST',
url: 'cms/index.php?view=joint_reg',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).fadeIn('fast');
if ($('form #response').hasClass('success')) {
setTimeout("$('form #response').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('form #response').removeClass().addClass('error')
.html('<p>There was an<strong> ' + errorThrown +
'</strong> error due to a<strong> ' + textStatus +
'</strong> condition.</p>').fadeIn('fast');
},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};
谢谢大家的帮助。我喜欢 jquery,真的很想学习。