【问题标题】:php zend framework json Decoding failedphp zend框架json解码失败
【发布时间】:2012-02-01 21:26:47
【问题描述】:

我正在使用 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
(
)

【问题讨论】:

  • 您的 JSON 字符串格式是否正确?
  • $data 实际上是否包含 JSON 字符串? PHP 的原生 json_decode() 可以工作吗? JSON 字符串中是否有多字节/非 ascii 字符?
  • 您的编辑不正确。同样,您没有引用您的对象属性名称。此外,字符串需要使用 quotes 而不是 撇号。 PHP 对其 JSON 语法非常挑剔。您编辑的 AJAX 调用的数据应该如下{"userId": "111", "imageUrl": "imageurl", "message": "message" } 是合适的测试。
  • 仍然不正确。你不是在看我的代码是如何格式化的。除非您注意 JSON 的语法(没有撇号、引用字符串、引用对象属性名称),否则它不会在 PHP 中正确解析。

标签: php jquery ajax json zend-framework


【解决方案1】:

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

?> 

使用 json_decode() 的示例 #3 常见错误

<?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 — 返回最后发生的错误

得到错误。

【讨论】:

  • 我认为问题在于 $data = $this->_request->getPost();返回一个数组而不是字符串,这就是为什么 Zend_Json::decode($data, true);抛出错误:(
  • @gruber 是的,但是 $this_request->getPost('data') 应该有一个 json 字符串。或者你可以试试 $this->_request->getRawBody(),它会返回原始的帖子数据。
【解决方案2】:

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

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

【讨论】:

  • PHP 接收什么?你能发个print_r($data); 给我们看看结果吗?
  • 您的编辑再次出错。您编辑的 AJAX 调用看起来不像我上面的。请注意,我的整个数据部分没有引号,而且我的数据中也没有一个撇号。
  • 可以吗? $.ajax({ url: 'testURL', type: 'POST', dataType: 'json', data: {"userId": "test1", "imageUrl": "test2", "message": "test3" } , 成功:callBack });
  • 是的。但是,此时它是发布数据,而不是json_decode()d。您可以使用$data 作为数组访问它。
【解决方案3】:

我建议如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2010-11-16
    相关资源
    最近更新 更多