【问题标题】:Echo $_POST with $.ajax in PHP在 PHP 中使用 $.ajax 回显 $_POST
【发布时间】:2016-01-25 09:53:04
【问题描述】:

我正在尝试在 PHP 中使用$.ajax 使用echo $_POST,但没有成功。在 Xdebug 中,我看到 $_POST 获得了正确的值并执行了 echo $_POST 行,但我不断收到 else 输出子句。同样在 chrome 中,我看到发送的标题有效。所有代码都在同一个页面 index.php 中。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery.post demo</title>
    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
</head>
<body>

<button type="button" id="but">Click Me!</button>

<script>
    $('#but').click(function() {
        $.ajax({
            type: "POST",
            url: "index.php",
            data: {name: "John"},
            success: function() {
                alert('success');
            }
        });
    });
</script>
</body>
</html>

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    echo $_POST['name'];
} else {
    echo "Nothing to Show";
}
?>

【问题讨论】:

  • index.php 和 HTML 脚本是否在同一个文件中?
  • 把PHP代码放到自己的页面中。
  • @wizard 当您在 ajax 中调用 index.php 时,它将返回顶部的所有 HTML 代码。最好把php放到一个单独的文件里调用。
  • 要添加到上述 cmets,您不会在 ajax 调用中对 php 脚本的输出做任何事情,因此您只会看到您的 success 警报。而当你重新加载页面时,它又会是一个 GET 请求。如果您在 succes 函数中显示返回值,您将看到页面的完整 html 源代码,末尾带有文本 John
  • @wizard 显然你关心返回值,否则就没有理由echo 任何东西。

标签: php jquery json ajax post


【解决方案1】:

目前整个页面正在返回以响应您的 AJAX 请求; HTML 和所有。您也没有在 JS 代码中检索从请求返回的值,为此您只需要在 success 处理程序上接受一个参数。试试这个:

在它自己的文件中,说foo.php

<?php
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        echo $_POST['name'];
    } else {
        echo "Nothing to Show";
    }
?>

然后在你的 HTML 中:

<script>
    $('#but').click(function() {
        $.ajax({
            type: "POST",
            dataType: 'text', // to ensure jQuery doesn't try to deserialise the repsonse
            url: "foo.php",
            data: { name: "John" },
            success: function(response) {
                console.log(response.trim()); // = 'John'
            }
        });
    });
</script>

【讨论】:

  • 还是不好。我想提一下,我现在不太关心回应。我只是希望能够回显 $_POST 请求。
  • 但这没有任何意义 - 查看您 拥有 的回显 PHP 变量以从 AJAX 请求中获取响应。这应该可以正常工作 - 检查控制台是否有错误。
  • 我理解你,但我想再次使用 PHP 来回显结果。同样,从表单提交中回显 $_POST 结果(开箱即用)。
  • 您似乎对 AJAX 的工作原理感到困惑。您正在通过 PHP 回显结果,然后由触发请求的 JS 读取。
【解决方案2】:

Ajax 将返回一个响应,您可以使用该响应进行显示。它不能在同一页面上工作。

尝试如下:

main.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery.post demo</title>
    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
</head>
<body>
<button type="button" id="but">Click Me!</button>
<div id="result"></div>
<script>
    $('#but').click(function() {
        $.ajax({
            type: "POST",
            url: "test.php",
            data: {name: "John"},
            success: function(data) {
                alert('success');
                $('#result').html(data);
            }
        });
    });
</script>
</body>
</html>

test.php

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    echo $_POST['name'];
} else {
    echo "Nothing to Show";
}
?>

【讨论】:

  • 是的,但我想使用 PHP 回显 $_POST。
猜你喜欢
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多