功能是:
<script type="text/javascript">
jQuery(function () {
jQuery("#JqPostForm").submit(function () {
jQuery.post("index2.php?option=compon&task=sendemail", jQuery("#JqPostForm").serialize(),
function (data) {
alert(data.email_invalid);
if (data.email_invalid == 'true') {
jQuery("#message_post").append("Wrong email");
} else {
jQuery("#message_post").html("<div class='successMessage'>" + data.email + " is a valid e-mail address. Thank you, " + data.name + ".</div>");
}
},
function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}, "json");
return false;
});
});
</script>
返回json数据{“email_invalid”:“true”}
但是在访问alert(data.email_invalid)时;我有未定义的吗?
答案 0 :(得分:1)
您的dataType
应该是一个错误处理程序集。您无法使用$.post
设置错误处理程序;您需要使用$.ajax
代替:
$.ajax({
type: 'POST',
url: "index2.php?option=compon&task=sendemail",
data: jQuery("#JqPostForm").serialize(),
success: function (data) {
alert(data.email_invalid);
if (data.email_invalid == 'true') {
jQuery("#message_post").append("Wrong email");
} else {
jQuery("#message_post").html("<div class='successMessage'>" + data.email + " is a valid e-mail address. Thank you, " + data.name + ".</div>");
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
},
dataType: 'json'
});