php zend框架json解码失败

时间:2012-02-01 21:26:48

标签: php jquery ajax json zend-framework

我正在使用ajax:

$.ajax({
        url: 'testURL',
        type: 'POST',
        dataType: 'json',
        data: {userId: userIds, imageUrl: imageUrl, message: message },
        success: callBack
    });

和服务器端:

$data = $this->_request->getPost();
        $response = Zend_Json::decode($data, true);

但我在服务器端遇到错误:

Decoding failed

我的错误是什么?

感谢您的帮助

编辑:

我试过了:

$.ajax({
url: STValentines.baseUrl+'/mensaje/sendmessage',
type: 'POST',
dataType: 'json',
data: {userId: '111', imageUrl: 'imageurl', message: 'message' },
success: callBack
}); 

同样的错误

编辑2:

这里再次是js代码php代码和结果:(

 $.ajax({
        url: 'testURL',
        type: 'POST',
        dataType: 'json',
        data: "{'userId': 'test1234', 'imageUrl': 'testimageUrl', 'message': 'testmessage' }",
        success: callBack
    });


 public function sendmessageAction() {
    $data = $this->_request->getPost();
    print_r($data);
    $response = $data;
$this->_helper->json($response);

结果:

    Array
(
)

3 个答案:

答案 0 :(得分:3)

Crashspeeder至少应该以他的数据格式正确。

  

来自PHP手册 - json_decode - 解码JSON字符串

//correct json format
Example #1 json_decode() examples


<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?> 

  

示例#3使用json_decode()

的常见错误
<?php

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null

?> 

你也可以使用......

  

json_last_error - 返回上次发生的错误

得到错误。

答案 1 :(得分:1)

乍一看,您发送的数据可能不正确。如果我没记错,需要引用对象属性。试试这个。

$.ajax({
    url: 'testURL',
    type: 'POST',
    dataType: 'json',
    data: {"userId": userIds, "imageUrl": imageUrl, "message": message },
    success: callBack
});

答案 2 :(得分:0)

我建议如下:

  1. 从AJAX请求中删除dataType: 'json'
  2. 在操作中,使用return $this->_helper->json($responseArray);。无需更改布局或任何内容。