我有一个和这个非常相似的问题:jquery json function returning null。在jQuery.ajax中返回'null'成功方法的PHP函数调用
但是,我遵循了上述建议,因此我仍然看到null
。
这里是我的代码:
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'
));
}
}
控制台/调试器/网络面板均说,我正确地说话Ajax控制器但是,成功方法的data
只返回null。
我相当新手到PHP,任何建议非常感谢。
谢谢你,肯
UPDATE
我调用仍然返回null,所以我想我会在这里贴上我的头。
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
感谢所有答案的家伙,现在修改我的代码。将尽快更新。 – Ken 2012-03-05 06:39:56