2012-03-05 68 views
3

我有一个和这个非常相似的问题: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 
+0

感谢所有答案的家伙,现在修改我的代码。将尽快更新。 – Ken 2012-03-05 06:39:56

回答

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

OR

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' 
] 
+0

换句话说,从''1'=>'等移除单引号,?我这样做,仍然返回null。不知道为什么我在那里有那些报价。 – Ken 2012-03-05 06:38:03

1

您使用的一部分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

+0

仍然返回null。我正在开发本地在我自己的虚拟主机在Apache'http://local.sitename.dev'可能会导致'同源政策'的问题? – Ken 2012-03-05 06:44:53

+0

@ken:没有那么长时间,您正在使用相同的域名访问该网站。 – prodigitalson 2012-03-05 12:57:53

3

回声在constructor.i年底认为你在控制犯规回声东西,所以Ajax响应为空。 whioch框架你使用?

+0

我不太清楚你的意思。你可以在代码中显示它吗? – Ken 2012-03-05 06:43:25

+0

其实我甚至没有明白。尽管如此,它仍然应该回应,但它有可能引发问题。 +1 – prodigitalson 2012-03-05 06:44:52

+0

如果你可以提供一个你的意思的例子,我会是伟大的。仍然在那里的'回声'逻辑混淆。 – Ken 2012-03-05 06:51:56

2

在你的/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(); 
+0

很棒! - 仍然返回null,但:( – Ken 2012-03-05 17:46:44

0

我终于在头部周围挖了一圈,意识到问题出在那。 ajax()正在向我的PHP脚本发送一个空数组。

该修复是为了摆脱contentType: 'application/json; charset=utf-8和PHP脚本中的header()函数。

它现在发送和接收数据就好了。 (去上配置文档读了)

感谢所有帮助 - 我的脚本会;一直在打破其他原因W/O很多你的答案:)

相关问题