带返回的Ajax返回值不起作用 [英] Ajax return value with return not work

查看:43
本文介绍了带返回的Ajax返回值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件(call.php和post.php),并使用ajax从调用到传递值,我想从post获取返回值,但这不起作用.当我更改帖子时,将返回"修改为回声",它可以工作,但是我不知道为什么.有人可以给我帮助吗?
实例将是最大的赞赏.

I have 2 files(call.php and post.php) and using ajax pass value from call to post,and i want to get return value from post ,but this doesn't work. when i change post ,modify "return" to "echo",it works,but i don't know why.can anybody give me a help?
Examples would be most appreciated.

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时,返回"会触发.

by contrast when i use MVC "return" will fire.

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();
                }
            });

推荐答案

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

This is the expected behaviour, Ajax will get everything outputted to the browser.

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

return only works if you are using the returned value with another php variable or function.

简而言之,php和javascript无法直接通信,它们只能通过php回显或打印的内容进行通信.将Ajax或php与javascript一起使用时,应使用echo/print而不是return.

In short, php and javascript can't communicate directly, they only communicate through what php echoed or printed. When using Ajax or php with javascript you should use echo/print instead of return.

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

In Fact, as far as I know, return in php is not even used in the global scope very often (on the script itself) it's more likely used in functions, so this function holds a value (but not necessarily outputs it) so you can use that value within 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将此值导入视图.


It's working on the MVC framework because that has several layers, probably the delete() method is a method from the model, which returns its value to the controller, and the controller echo this value into the view.

这篇关于带返回的Ajax返回值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆