简体   繁体   中英

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. The AJAX is calling it and returning, getting a null value for the response variable. Any Ideas? Here is the AJAX code:

$.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:

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. 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"). Click the row for the request and check the Response tab to make sure your JSON is there.

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). Otherwise, it would seemingly be a problem with your JavaScript.

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

echo $printing;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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