简体   繁体   English

我的jQuery,AJAX调用在从PHP函数有效返回格式良好的JSON数据时获得null响应

[英]My jQuery, AJAX call is getting a null response on a valid return of well-formatted JSON data from a PHP function

I have checked the logs of my PHP function and I have correctly formatted JSON data that I am returning from the method. 我已经检查了我的PHP函数的日志,并且我正确地格式化了我从该方法返回的JSON数据。 The AJAX is calling it and returning, getting a null value for the response variable. AJAX正在调用它并返回,为响应变量获取null值。 Any Ideas? 有任何想法吗? Here is the AJAX code: 这是AJAX代码:

$.ajax({
       type: "POST",
       url: "index.php/controllerFile/get_standby",
       data: 'id=' + $(this).attr('id'),
       success: function(response){  


             console.log('response is: ' + response); //It is null here
         $.colorbox({'href':'index.php/config/view/standby' + response.urlData,'width':1000,'title':'Standby People'});         
       },

       dataType:'json'
     });

Here is the PHP function: 这是PHP函数:

function get_standby()
    {

        $id = $this->input->post('id');

        $this->load->model('teetime');
        $url['urlData'] = ($this->teetime->get_standby_by_id($id));

        $printing = json_encode($url);
        log_message('error', 'JSON ' . $printing);
        return $printing;

    }

I would suggest opening up Developer Tools in Chrome (View > Developer > Developer Tools) and selecting the Network tab. 我建议在Chrome中打开开发人员工具(查看>开发人员>开发人员工具)并选择网络选项卡。 When your AJAX post request is made, it should add an entry there (the "Path" column should be "index.php/controllerFile/get_standby" and the "Method" column should have "POST"). 当你的AJAX发布请求时,它应该在那里添加一个条目(“Path”列应该是“index.php / controllerFile / get_standby”,“Method”列应该有“POST”)。 Click the row for the request and check the Response tab to make sure your JSON is there. 单击请求的行并选中Response选项卡以确保您的JSON在那里。

If the response is empty, your problem is with your PHP code (you might not be printing the JSON returned from that function to the page). 如果响应为空,则问题出在PHP代码上(您可能无法将从该函数返回的JSON打印到页面)。 Otherwise, it would seemingly be a problem with your JavaScript. 否则,它似乎是您的JavaScript的问题。

尝试在PHP中使用echo而不是return。

echo $printing;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM