Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryajax发布数据未定义的裸体示例_Javascript_Php_Html_Jquery_Ajax - Fatal编程技术网

Javascript jqueryajax发布数据未定义的裸体示例

Javascript jqueryajax发布数据未定义的裸体示例,javascript,php,html,jquery,ajax,Javascript,Php,Html,Jquery,Ajax,HTML: <html> <head> <title>Page Title</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="script.js"></s

HTML:

<html>
   <head>
      <title>Page Title</title>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

      <script src="script.js"></script>
   </head>
   <body>
      <button onclick="Posty()">click</button>
      <p id="result"></p>
   </body>
</html>
function Posty() {
   var pointless = "i am indeed";

   $.post("server.php", pointless, PostyReturned());
}

function PostyReturned(data) {
   var output = document.getElementById("result");

   output.innerHTML = data;
}
<?php 
$array = [
   "foo" => "bar",
   "bar" => "foo",
];

echo(json_encode($array));
die();
?>
server.php:

<html>
   <head>
      <title>Page Title</title>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

      <script src="script.js"></script>
   </head>
   <body>
      <button onclick="Posty()">click</button>
      <p id="result"></p>
   </body>
</html>
function Posty() {
   var pointless = "i am indeed";

   $.post("server.php", pointless, PostyReturned());
}

function PostyReturned(data) {
   var output = document.getElementById("result");

   output.innerHTML = data;
}
<?php 
$array = [
   "foo" => "bar",
   "bar" => "foo",
];

echo(json_encode($array));
die();
?>
我知道这个错误非常简单,但是我已经阅读了我能找到的每一个相关的错误帖子,我已经阅读了所有的文档,没有一个有帮助,所以我现在自己发布它,希望结束我的痛苦


提前欢呼。

看起来您需要将post结果中的数据传递到
PostyReturned()
函数中。我不知道全部细节,但问题的根源在于这些函数的执行顺序。
$.post
函数是异步的,您的函数是同步的。我发现这比我现在所能解释的更能帮助我们解决这个问题

我调整了代码,使success函数将post结果中的数据传递到PostyReturned()函数中。现在,它可以按预期工作:

     function Posty() {
            var pointless = "i am indeed";
            $.post("server.php", pointless, function(data) {PostyReturned(data)});
        }

        function PostyReturned(data) {
            var output = document.getElementById("result");
            output.innerHTML = data;
        }
调整功能的结果:


谢谢Shakima,你的解决方案很棒。碰巧我在看到你的答复之前就解决了这个问题。我相信正如您所说,当我编写PostyReturned()时,它是直接同步调用函数,而不是按照预期将其指定为success函数。您的解决方案将success函数指定为调用PostyReturned的函数,该函数工作正常

但是,更好的解决方案是删除函数名后的括号,这样它就不会立即调用函数,而是只提供一个引用

function Posty() {
    var pointless = "i am indeed";

    $.post("server.php", pointless, PostyReturned);
}

function PostyReturned(data) {
    var output = document.getElementById("result");

    output.innerHTML = data;
}
这段代码按照我最初的意图工作。耶:)