jquery post返回undefined

时间:2010-10-21 11:45:28

标签: jquery

功能是:

<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)时;我有未定义的吗?

1 个答案:

答案 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'
});