PHP函数返回';null';以获取jQuery.ajax调用中的成功方法


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

我遇到了一个与此问题非常相似的问题:jquery json函数返回null。

然而,我遵循了上面包含的建议,因此仍然看到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控制器的对话是正确的,然而,success方法的data只返回null。

我是PHP的新手,任何建议都非常感谢。

谢谢Ken

更新

我的呼叫仍然返回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

在构造函数的末尾回显。我认为您在控制器中没有回显任何内容,所以ajax响应为空。你使用的是什么框架?

我认为这不会产生有效的JSON。如果你想要一个带有数字键的实际数组,那么在PHP数组中使用数字键:

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'
));

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

它将输出以下js:

[
  'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
  'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
]

在/controllers/ajax.php文件中,是否正在运行函数?

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();

您正在使用url 的一部分

url: '/controllers/ajax.php',

尝试使用完整的url

var baseurl = 'http://www.example.com';
url: baseurl+'/controllers/ajax.php',

编辑

尝试更改

header('Content-type: application/json')

header('Content-type: text/json');

header('Content-type: text/plain');

https://stackoverflow.com/questions/267546/correct-http-header-for-json-file

我终于找到了标题,并意识到问题就在这里。ajax()向我的PHP脚本发送了一个空数组。

修复方法是去掉PHP脚本中的contentType: 'application/json; charset=utf-8header()函数。

它现在发送和接收数据都很好。(转到读取配置文档

感谢所有的帮助——我的剧本会的;由于其他原因,你的许多答案都被破坏了:)