php zend框架json解码失败

时间:2022-10-10 12:44:10

Im using ajax:

我使用ajax:

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

and server side:

和服务器端:

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

But Im getting an error on server side:

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

Decoding failed

What am i doiung wrong ?

我错了什么?

Thanks for any help

谢谢你的帮助

EDIT:

Ive tried that:

我试过了:

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

the same error

同样的错误

EDIT 2:

Here is once again js code php code and the result :(

这里再次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);

RESULT:

    Array
(
)

3 个解决方案

#1


3  

Crashspeeder should be correct at least in his data format.

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

from the PHP manual - json_decode — Decodes a JSON string

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

?> 

and

Example #3 common mistakes using json_decode()

示例#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

?> 

also you can use...

你也可以用......

json_last_error — Returns the last error occurred

json_last_error - 返回上次发生的错误

to get error.

得到错误。

#2


1  

At first glance, it looks like the data you're sending might be incorrect. If I remember correctly object properties need to be quoted. Try this.

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

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

#3


0  

I suggest the following:

我建议如下:

  1. Remove dataType: 'json' from the AJAX request.
  2. 从AJAX请求中删除dataType:'json'。

  3. In the action, use return $this->_helper->json($responseArray);. No need to change layout or anything.
  4. 在动作中,使用return $ this - > _ helper-> json($ responseArray);.无需更改布局或任何内容。

#1


3  

Crashspeeder should be correct at least in his data format.

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

from the PHP manual - json_decode — Decodes a JSON string

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

?> 

and

Example #3 common mistakes using json_decode()

示例#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

?> 

also you can use...

你也可以用......

json_last_error — Returns the last error occurred

json_last_error - 返回上次发生的错误

to get error.

得到错误。

#2


1  

At first glance, it looks like the data you're sending might be incorrect. If I remember correctly object properties need to be quoted. Try this.

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

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

#3


0  

I suggest the following:

我建议如下:

  1. Remove dataType: 'json' from the AJAX request.
  2. 从AJAX请求中删除dataType:'json'。

  3. In the action, use return $this->_helper->json($responseArray);. No need to change layout or anything.
  4. 在动作中,使用return $ this - > _ helper-> json($ responseArray);.无需更改布局或任何内容。