Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.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
Php 如何在jQuery.post()中获取返回值_Php_Jquery_Ajax - Fatal编程技术网

Php 如何在jQuery.post()中获取返回值

Php 如何在jQuery.post()中获取返回值,php,jquery,ajax,Php,Jquery,Ajax,如何获取返回值 单击“我的”按钮可调用: $("#generateNumber").click(function(){ $.post( "./result.php", function( data ) { console.log( data ); $(".generate_number").html(data); }); }); my result.php: <?php $a = "returnedValue"; ?> 但是我的控制

如何获取返回值

单击“我的”按钮可调用:

$("#generateNumber").click(function(){
   $.post( "./result.php", function( data ) {
      console.log( data );
      $(".generate_number").html(data);
   });
});
my result.php:

<?php 
   $a = "returnedValue";
?>


但是我的控制台日志没有显示我返回的值。为什么不呢?

您需要将值转储到
result.php

<?php
    $a = "returnedValue";
    echo $a;
?>
<?php
    function foo() { return "foo"; }
    function bar() { return "bar"; }

    switch($_POST['functionName']) {
        case 'foo': echo foo(); break;
        case 'bar': echo bar(); break;
    }
?>
result.php
中:

<?php
    $a = "returnedValue";
    echo $a;
?>
<?php
    function foo() { return "foo"; }
    function bar() { return "bar"; }

    switch($_POST['functionName']) {
        case 'foo': echo foo(); break;
        case 'bar': echo bar(); break;
    }
?>


回显
$a
<代码>echo$a=“returnedValue”仅回显还是返回?我如何才能在.post()url中运行?我不确定@user1638121,你能重新措辞吗?在回调时。我的post只能得到echo?或者可能是函数foo(){$a=“str”return$a},如果我可以在url$.post(“./result.php/howGetFunction”)中使用函数how to函数,函数(data){@user1638121查看我的更新。您可以将任何
$\u post
参数发送到php脚本,并使用它调用不同的函数以获得不同的返回。