【问题标题】:jQuery Ajax success function returns null?jQuery Ajax 成功函数返回null?
【发布时间】:2010-04-06 10:14:14
【问题描述】:

我使用以下 jquery 语句来调用我的 php 控制器函数,它被调用但我的结果没有返回到我的成功函数......

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/style.css" />
        <link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/calendar.css" />
        <link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/date_picker.css" />
        <script type="text/javascript" src="http://localhost/codeigniter_cup_myth/javascript/jquery1.4.2.js"></script>
        <script type="text/javascript" src="http://localhost/codeigniter_cup_myth/javascript/jquery.pagination.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                getRecordspage();
            });

            function getRecordspage() {
                $.ajax({
                    type: "POST",
                    url:"http://localhost/codeigniter_cup_myth/index.php/adminController/mainAccount",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    global:false,
                    async: true,
                    dataType: "json",
                    success: function(result) {
                        alert(result);
                    }
                });
            }
        </script>
    </head>
    <body>
        <table  id="chkbox" cellpadding="0" cellspacing="2" width="100%" class="table_Style_Border">
            <tr>
                <td class="grid_header" align="center">S.No</td>
                <td class="grid_header" align="center">Account Name</td>
                <td class="grid_header" align="center">Account Acronym</td>
                <td class="grid_header" align="center">Finance Year Start</td>
                <td class="grid_header" align="center">Finance Year End</td>
                <td class="grid_header" align="center">&nbsp;</td>
            </tr>
            <tr> <td colspan="5"> </td></tr>
        </table>
    </body>
</html>

我的控制器方法,

function mainAccount()
{
    $_SESSION['menu'] = 'finance';
    $data['account'] = $this->adminmodel->getaccountDetails();
    if(empty($data['account']))
    {
        $data['comment'] = 'No record found !';
    }
    $json = json_encode($data);
    return $json;
}

我在成功函数中得到alert(1);,但我的alert(result); 显示null。我该如何解决这个问题?

这是我给print_r($data);时得到的:

Array ( [account] => Array ( [0] => Array ( [dAcc_id] => 1 [dAccountName] => Govt. College Of Technology [dAccountAcronym] => GCT [dFromDate] => 2010-04-02 [dToDate] => 2011-05-03 ) [1] => Array ( [dAcc_id] => 3 [dAccountName] => sample4 [dAccountAcronym] => smp_4 [dFromDate] => 2010-03-17 [dToDate] => 2011-03-03 ) [2] => Array ( [dAcc_id] => 4 [dAccountName] => sample3 [dAccountAcronym] => smp_3 [dFromDate] => 2010-03-16 [dToDate] => 2011-03-17 ) [3] => Array ( [dAcc_id] => 5 [dAccountName] => sample5 [dAccountAcronym] => smp_5 [dFromDate] => 2010-03-12 [dToDate] => 2011-03-03 ) [4] => Array ( [dAcc_id] => 6 [dAccountName] => sample2 [dAccountAcronym] => smp2 [dFromDate] => 2010-03-01 [dToDate] => 2011-03-16 ) [5] => Array ( [dAcc_id] => 7 [dAccountName] => sample1 [dAccountAcronym] => smp_1 [dFromDate] => 2010-03-11 [dToDate] => 2011-03-03 ) [6] => Array ( [dAcc_id] => 8 [dAccountName] => ss [dAccountAcronym] => ss [dFromDate] => 2010-04-04 [dToDate] => 2010-04-06 ) ) )

当我做print_r(json_encode($data['account'])); 时,我得到了这个:

[{"dAcc_id":"1","dAccountName":"Govt. College Of Technology","dAccountAcronym":"GCT","dFromDate":"2010-04-02","dToDate":"2011-05-03"},{"dAcc_id":"3","dAccountName":"sample4","dAccountAcronym":"smp_4","dFromDate":"2010-03-17","dToDate":"2011-03-03"},{"dAcc_id":"4","dAccountName":"sample3","dAccountAcronym":"smp_3","dFromDate":"2010-03-16","dToDate":"2011-03-17"},{"dAcc_id":"5","dAccountName":"sample5","dAccountAcronym":"smp_5","dFromDate":"2010-03-12","dToDate":"2011-03-03"},{"dAcc_id":"6","dAccountName":"sample2","dAccountAcronym":"smp2","dFromDate":"2010-03-01","dToDate":"2011-03-16"},{"dAcc_id":"7","dAccountName":"sample1","dAccountAcronym":"smp_1","dFromDate":"2010-03-11","dToDate":"2011-03-03"},{"dAcc_id":"8","dAccountName":"ss","dAccountAcronym":"ss","dFromDate":"2010-04-04","dToDate":"2010-04-06"}]

【问题讨论】:

  • 结果正文可以null,如果标头为200 OK,仍然是有效结果。您是否检查过您的网址实际上返回了什么?
  • @pekka 我通过萤火虫检查了我的响应选项卡没有任何内容....
  • @pekka 我的帖子有这个JSON Source {}
  • @udaya 然后响应为空,null 显示正确 - 奇怪,因为在您的代码中,这不可能真正发生。
  • @pekka 有什么建议吗?

标签: javascript jquery ajax codeigniter


【解决方案1】:

您是否正确设置了内容类型?

header('Content-Type: application/json');

使用 CodeIgniter,您是要返回 JSON 对象还是输出它?如果没有与该方法关联的视图,则不会输出任何内容。试一试,看看它是否有效:

$_SESSION['menu'] = 'finance';
$data['account'] = $this->adminmodel->getaccountDetails();
if (empty($data['account'])) {
  $data['comment'] = 'No record found !';
}
header('Content-Type: application/json');
echo json_encode($data);
exit;

最后,验证你要访问的 URL,看看它是否返回了一些东西。

看看JSON Helper

【讨论】:

  • @cletus 当我 print_r(json_encode($data)); 我得到 json 字符串但为什么不能将它返回到我的 ajax 成功函数...
  • @udaya - @cletus 的意思是,尝试使用echo json_encode($data); 而不是return $data
【解决方案2】:

发生这种情况的最常见原因是,如果您是一个非安全页面,试图通过 ajax 与安全页面进行通信,反之亦然(即 http ajaxing https)

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2015-04-14
    相关资源
    最近更新 更多