【问题标题】:PHP function returning 'null' for success method in jQuery.ajax callPHP函数在jQuery.ajax调用中为成功方法返回'null'
【发布时间】:2012-03-05 06:26:16
【问题描述】:

我有一个与此非常相似的问题:jquery json function returning 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 新手,非常感谢任何建议。

谢谢,肯

更新

我的电话仍然返回 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

【问题讨论】:

  • 感谢大家的回答,现在修改我的代码。很快就会更新。

标签: php jquery ajax json


【解决方案1】:

在构造函数的末尾回显。我认为您在控制器中没有回显任何内容,因此 ajax 响应为空。 你用的哪个框架?

【讨论】:

  • 其实我什至没听懂。它仍然应该回声,但是它有可能导致问题。 +1
  • 如果你能提供一个例子来说明你的意思,我会非常棒。仍然对那里的echo 逻辑感到困惑。
  • 为了从 php 到 ajax 到 js 获取数据,你必须在 php 文件中回显/打印。所以它可以传输到 js.in __construct 只需写在函数 echo 'test' 的末尾;跨度>
  • 哦,是的..我没看到你这么叫。我很抱歉
  • 呵呵一切都很好,关于为什么它仍然可能返回 null 的想法?
【解决方案2】:

我认为这不会产生有效的 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'
]

【讨论】:

  • 换句话说,从'1' => 等中删除单引号,?我这样做了,仍然返回null。不知道为什么我一开始就在那里引用。
【解决方案3】:

在您的 /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();

【讨论】:

  • 伟大的收获! - 但是仍然返回 null :(
【解决方案4】:

您正在使用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

【讨论】:

  • 仍然返回 null。我在我自己的 apache 'local.sitename.dev' 的 vhost 上本地开发这个可能会导致 'same-origin-policy' 问题吗?
  • @ken:只要你访问的是同一个域名的网站。
【解决方案5】:

我终于挖掘了标题并意识到问题是 .ajax() 正在向我的 PHP 脚本发送一个空数组。

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

它现在可以正常发送和接收数据了。 (去阅读配置文档

感谢您的所有帮助 - 我的脚本会因为其他原因而被破坏而没有您的许多答案 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2015-04-14
    • 1970-01-01
    • 2023-03-29
    • 2011-04-30
    相关资源
    最近更新 更多