Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function returning 'null' for success method in jQuery.ajax call

I have an issue very similar to this one: jquery json function returning null.

I have followed the above contained advice, however, and am still seeing null as a result.

Here is my code:

JS:

Gallery.prototype.getImages = function(opt){
  var self = this;
      var baseurl = 'http://local.gallery.dev'
  $.ajax({
    url: baseurl+='/controllers/ajax.php',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: {action : opt},
    dataType: 'JSON',
    success: function(data){
        //self.setImages(data);
        console.log(data)
    },
    error: function(){
        console.log('NOPE');
    }
  });
}

PHP:

class ajax_controller {

function __construct(){

    if(isset($_POST['action']) && !empty($_POST['action'])) {
        $action = $_POST['action'];
        switch($action) {
            case 'Newborn' : $this->Newborn();
                break;
        }
    }
}
/*
 * Process Newborn Gallery request.
 */
public function Newborn(){
    header('Content-type: application/json');
    echo json_encode(array(
      'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
      'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
    ));
}
}

The console/debugger/network panel are all saying that I am talking to the ajax controller correctly, however, the data of the success method only returns null.

I am fairly novice to PHP, any suggestions greatly appreciated.

Thanks, Ken

UPDATE

My call is still returning null so I thought i'd paste my headers here.

Request URL:http://local.sunnyrose.dev/controllers/ajax.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
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:14
Content-Type:application/json; charset=UTF-8
Host:local.sunnyrose.dev
Origin:http://local.sunnyrose.dev
Referer:http://local.sunnyrose.dev/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML,              

like Gecko) Chrome/17.0.963.56 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
action=Newborn
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json
Date:Mon, 05 Mar 2012 17:49:53 GMT
Keep-Alive:timeout=5, max=92
Server:Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4       

Perl/v5.10.1
X-Powered-By:PHP/5.3.1
like image 360
Ken Avatar asked Mar 11 '26 23:03

Ken


2 Answers

echo at the end of constructor.i think you doesnt echo anything in controller , so ajax response is null. whioch framework u use?

like image 175
sandeep Avatar answered Mar 14 '26 11:03

sandeep


I dont think thats going to produce valid JSON. If you want an actual array with numeric keys then use numeric keys in the PHP array:

echo json_encode(array(
  1 => 'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
  2 => 'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));

OR

echo json_encode(array(
  'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
  'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));

which will output the following js:

[
  'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
  'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
]
like image 22
prodigitalson Avatar answered Mar 14 '26 13:03

prodigitalson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!