无法检索函数预期返回值

时间:2015-08-17 01:46:02

标签: javascript php ajax

如何从jquery ajax调用函数无法返回预期值时解决这个问题?

我尝试使用控制台来查看是否有错误,但我没有发现任何错误

这是我的javascript代码

function fnGetApplication(){
                var req=$.ajax({
                    url: 'classes/BLL/masterBLL.php/GetApplication',
                    method: 'post',
                    datatype: 'json',
                    beforeSend:function(){
                        dialog('.dialogpreloader');
                    },
                    complete:function(){
                        dialog('.dialogpreloader');
                    }
                });

                req.done(function(data){
                    $('#txtacyear').val(data[0].acyear);
                    $('#txtacmonth').val(data[0].acmonth);
                    $('#txtappcode').val(data[0].appcode);
                });

                req.fail(function(request,status,error){
                    notify('alert', 'Alert', request.responseText, '<span class="mif-notification"></span>');
                });
            }

这是我的PHP代码

    <?php
header("Content-Type: application/json");

include '../DAL/masterDAL.php';
include '../MDL/applicationMDL.php';

class MasterBLL{
    public function GetApplication(){
        $fn = new MasterDAL();
        $res= $fn->GetApplicationPeriod()->fetchAll(); 

        return json_encode($res);
    }
    public function SaveApplication(){
        $params = ($_POST['params']);

        $application=new ApplicationMDL();
        $application->acyear=$params['acyear'];
        $application->acmonth=$params['acmonth'];
        $application->appcode=$params['appcode'];

        $fn = new MasterDAL();
        $res= $fn->SaveApplicationPeriod($application)->fetch(PDO::FETCH_ASSOC); 

        return json_encode($res);
    }
}

?>

我在标题中使用谷歌浏览器:

  Request URL:http://localhost/website/classes/BLL/masterBLL.php/GetApplication
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:OFBiz.Visitor=10000
Host:localhost
Origin:http://localhost
Referer:http://localhost/website/index.php?page=appperiod
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json
Date:Mon, 17 Aug 2015 02:25:09 GMT
Keep-Alive:timeout=5, max=86
Server:Apache/2.4.9 (Win32) PHP/5.5.12
X-Powered-By:PHP/5.5.12

1 个答案:

答案 0 :(得分:0)

我建议您将return json_encode($res)更改为echo json_encode($res)