admin管理员组

文章数量:1317894

I use the following the jquery statements to call my php controller function, it gets called but my result is not returned to my success function....

<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>

My controller method,

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

I get the alert(1); in my success function but my alert(result); show null. How do I fix this problem?

This was what I got when I gave 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 ) ) )

When I did print_r(json_encode($data['account']));, I got this:

[{"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"}]

I use the following the jquery statements to call my php controller function, it gets called but my result is not returned to my success function....

<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>

My controller method,

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

I get the alert(1); in my success function but my alert(result); show null. How do I fix this problem?

This was what I got when I gave 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 ) ) )

When I did print_r(json_encode($data['account']));, I got this:

[{"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"}]
Share Improve this question edited Jun 26, 2015 at 16:33 Sumurai8 20.8k11 gold badges68 silver badges102 bronze badges asked Apr 6, 2010 at 10:14 udayaudaya 9,78815 gold badges50 silver badges69 bronze badges 15
  • The result body can be null and still be a valid result if the header is 200 OK. Have you checked your URL actually returns something? – Pekka Commented Apr 6, 2010 at 10:15
  • @pekka i inspected through firebug my response tab had nothing.... – udaya Commented Apr 6, 2010 at 10:18
  • @pekka my post had this JSON Source {} – udaya Commented Apr 6, 2010 at 10:19
  • @udaya then the response is empty, and the null appears correctly - strange because in your code, that can't really happen. – Pekka Commented Apr 6, 2010 at 10:20
  • @pekka any suggesstion how it can be done? – udaya Commented Apr 6, 2010 at 10:24
 |  Show 10 more ments

2 Answers 2

Reset to default 2

Have you set the content type correct?

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

With CodeIgniter, are you meant to return the JSON object or output it? If there's no view associated with the method then nothing will be output. Try, just to see if it works:

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

Lastly, verify the URL you are going to and see if it returns something.

Take a look at JSON Helper.

The most mon reason this happens is if you are a non-secure page trying to municate via ajax with a secure page, or vise versa (i.e. http ajaxing https)

本文标签: javascriptjQuery Ajax success function returns nullStack Overflow