参考错误:'数据'没有定义

时间:2014-07-04 09:19:35

标签: jquery ajax forms

大家好,感谢您阅读本文,

今天我遇到了一个问题,我想通过ajax提交一个html表单,但似乎这个表单中的数据没有传递给jquery。

我的设置如下:

我必须使用var jq = $.noConflict();来防止原型和jquery的冲突(所以jq只是意味着$)。

index.php - 通过content/pc-service.php

包含另一个.php页面(jq('#p2-content').load('content/pc-service.php');

content / pc-service.php - 看起来像这样:

<script>
    jq(function () {

        jq('form').on('submit', function (e) {

            e.preventDefault();

            jq.ajax({
                type: jq(this).attr("method"),
                url: jq(this).attr("action"),
                data: jq(this).serialize(),
                success: function () {
                    jq('#p2-content').html(data);
                    //alert('success');  // <-- this alert - when uncommented - actually fires on clicking submit!
                }
            });

        });

    });
</script>
<?php

if(!sizeof($_POST))
{

?>

[... some texts and stuff]

<form action="?p=pc-service" method="post" >
    <input type="hidden" value="hello" >
    <input type="submit" name="submit" >
</form>

<?php

}
elseif (isset($_POST['submit'])) {

?>

[... content that should be displayed on submit]

<?php

}

?>

因此,当我按下提交并取消注释警报时,如上所述,它会给我一个警告,但当我尝试在<div>内显示数据时,这是我在控制台中的结果:{{1} }

请告诉我我错在哪里,谢谢大家帮助我:)。

3 个答案:

答案 0 :(得分:2)

您未在success处理程序函数中包含该参数:

success: function (data) { // < add 'data' here
    jq('#p2-content').html(data);
}

答案 1 :(得分:0)

您在此处缺少参数,

success: function (_**PARAMETER**_) {
    jq('#p2-content').html(data);
    //alert('success');  
    // <-- this alert - when uncommented - actually fires on clicking submit!
}

答案 2 :(得分:0)

您缺少成功参数

success: function (data) {
    jq('#p2-content').html(data);
    //alert('success');  
    // <-- this alert - when uncommented - actually fires on clicking submit!
}
相关问题