Reputation: 548
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
Upvotes: 3
Views: 2086
Reputation: 548
I finally dug around the headers and realized the problem was that .ajax()
was sending my PHP script an empty array.
The fix was to get rid of contentType: 'application/json; charset=utf-8
, and the header()
function in the PHP script.
It now sends and receives data just fine. (goes to read up on configuration docs)
Thanks for all the help - my script would;ve been broken for other reasons w/o many of your answers :)
Upvotes: 0
Reputation: 318302
In your /controllers/ajax.php file are you running your functions?
class ajax_controller {
function __construct(){
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'Newborn' : return $this->Newborn();
break;
}
}
}
/*
* Process Newborn Gallery request.
*/
public function Newborn(){
header('Content-type: application/json');
return json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
}
}
$controller = new ajax_controller;
echo $controller->__construct();
Upvotes: 2
Reputation: 17586
you are using part of a url
url: '/controllers/ajax.php',
try using your full url
like
var baseurl = 'http://www.example.com';
url: baseurl+'/controllers/ajax.php',
EDIT
try changing
header('Content-type: application/json')
to
header('Content-type: text/json');
or
header('Content-type: text/plain');
https://stackoverflow.com/questions/267546/correct-http-header-for-json-file
Upvotes: 1
Reputation: 60403
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'
]
Upvotes: 2
Reputation: 2234
echo at the end of constructor.i think you doesnt echo anything in controller , so ajax response is null. whioch framework u use?
Upvotes: 3