【问题标题】:Ajax return value with return not workAjax返回值与返回不起作用
【发布时间】:2023-03-11 06:36:01
【问题描述】:

我有 2 个文件(call.php 和 post.php)并使用 ajax 将值从调用传递到 post,我想从 post 获取返回值,但这不起作用。当我更改帖子时,将“return”修改为“echo”,它可以工作,但我不知道为什么。有人可以帮我吗?
例子将不胜感激。

call.php

 <script type="text/JavaScript">
 $(document).ready(function(){
    $('#submitbt').click(function(){
    //var name = $('#name').val();
    //var dataString = "name="+name;
    var dataPass = {
            'name': $("#name").val()
        };
    $.ajax({
        type: "POST",
        url: "post.php",        
        //data: dataString,        
        data: dataPass,//json
        success: function (data) {            
            alert(data);
            var re = $.parseJSON(data || "null");
            console.log(re);    
        }
    });
   });
});
</script>

post.php:

<?php
    $name = $_POST['name'];
    return json_encode(array('name'=>$name));
?>

更新:

相比之下 当我使用 MVC 时“返回”会触发。

public function delete() {
        $this->disableHeaderAndFooter();

        $id = $_POST['id'];
        $token = $_POST['token'];

        if(!isset($id) || !isset($token)){
            return json_encode(array('status'=>'error','error_msg'=>'Invalid params.'));
        }

        if(!$this->checkCSRFToken($token)){
            return json_encode(array('status'=>'error','error_msg'=>'Session timeout,please refresh the page.'));
        }

        $theme = new Theme($id);        
        $theme->delete();

        return json_encode(array('status'=>'success')); 
    }



   $.post('/home/test/update',data,function(data){

                var retObj = $.parseJSON(data);

                //wangdongxu added 2013-08-02
                console.log(retObj);        

                //if(retObj.status == 'success'){
                if(retObj['status'] == 'success'){                  
                    window.location.href = "/home/ThemePage";
                }
                else{
                    $('#error_msg').text(retObj['error_msg']);
                    $('#error_msg').show();
                }
            });

【问题讨论】:

  • 在 PHP 中将内容放入流中时,请使用 echoreturn 用于 PHP 中的函数

标签: php ajax


【解决方案1】:

这是预期的行为,Ajax 会将所有内容输出到浏览器。

return 仅在您将返回值与另一个 php 变量或函数一起使用时才有效。

简而言之,php 和 javascript 不能直接通信,它们只能通过 php 回显或打印的内容进行通信。在使用 Ajax 或 php 和 javascript 时,您应该使用 echo/print 而不是 return。


事实上,据我所知,php 中的return 甚至不经常在全局范围内使用(在脚本本身上)它更有可能在函数中使用,所以这个函数持有一个值(但不一定输出它),以便您可以在 php 中使用该值。

function hello(){
    return "hello";
}

$a = hello();
echo $a; // <--- this will finally output "hello", without this, the browser won't see "hello", that hello could only be used from another php function or asigned to a variable or similar.

它在 MVC 框架上工作,因为它有几个层次,可能 delete() 方法是模型中的一个方法,它将其值返回给控制器,控制器 echo 将此值返回到视图中。

【讨论】:

  • 我确切地知道“返回”是如何工作的,你的回答与我的问题不符
【解决方案2】:

$.ajax() 中使用dataType 选项

dataType: "json"

post.php 中试试这个,

<?php
    $name = $_POST['name'];
    echo json_encode(array('name'=>$name));// echo your json
    return;// then return
?>

【讨论】:

  • 添加时在 $.ajax() 中使用这个选项 dataType: "json" ,还是不行
  • @wusuopubupt 你在alert 中得到data 吗?
  • 然后签入console,你的ajax requestpost.php是否被调用。
  • 你们完全没有抓住重点,ajax / jquery 部分很好。在这些情况下,您不必使用“return”。不知道谁赞成这个
猜你喜欢
  • 2016-07-30
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多