引用错误:“数据”没有定义

时间:2022-11-05 14:54:15

Hello guys and thank you for reading this,

大家好,感谢你们的阅读,

today I ran into the problem that I'd like to submit a html form via ajax but it seems that the data inside this form isn't passed on to jquery.

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

My setup is as following:

我的设置如下:

I have to use var jq = $.noConflict(); to prevent conflicts of prototype and jquery (so jq just means $).

我必须使用var jq = $.noConflict();为了防止原型和jquery之间的冲突(所以jq仅仅表示$)。

index.php - includes another .php page (content/pc-service.php) via jq('#p2-content').load('content/pc-service.php');

索引。php -包含另一个.php页面(内容/pc-service.php)通过jq('#p2-content').load('内容/pc-service.php');

content/pc-service.php - looks like this:

内容/ pc服务。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

}

?>

So when I press submit and the alert is uncommented, as stated above, it gives me an alert but when I try to display the data inside a <div> this is my result in the console: Uncaught ReferenceError: data is not defined

因此,当我按下submit并没有注释时(如上所述),它会给我一个警告,但当我尝试在

中显示数据时,这是我在控制台的结果:Uncaught ReferenceError: data未定义

Please tell me where I'm wrong and thank you all for helping me :)

请告诉我哪里做错了,谢谢大家的帮助。

3 个解决方案

#1


2  

You did not include the parameter in your success handler function:

您没有在成功处理器函数中包含参数:

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

#2


0  

You are missing success parameter,

你失去了成功参数,

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

#3


0  

You're missing the parameter here,

这里没有参数,

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

#1


2  

You did not include the parameter in your success handler function:

您没有在成功处理器函数中包含参数:

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

#2


0  

You are missing success parameter,

你失去了成功参数,

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

#3


0  

You're missing the parameter here,

这里没有参数,

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