2

Im using 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 :(

 $.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
(
)
4
  • is your JSON string in proper format? Commented Feb 1, 2012 at 21:31
  • Does $data actually contain a JSON string? Does PHP's native json_decode() work? Are there multibyte/non-ascii characters in the JSON string? Commented Feb 1, 2012 at 21:31
  • Your edits are incorrect. Again, you're not quoting your object property names. Also, strings need to use quotes not apostrophes. PHP is VERY picky about its JSON syntax. Your edited AJAX call's data should've been as follows {"userId": "111", "imageUrl": "imageurl", "message": "message" } to be a suitable test. Commented Feb 1, 2012 at 22:19
  • Still incorrect. You're not looking at how my code is formatted. Until you pay attention to the syntax of the JSON (no apostrophes, strings are quoted, object property names are quoted) it won't parse properly in PHP. Commented Feb 1, 2012 at 22:45

3 Answers 3

3

Crashspeeder should be correct at least in his data format.

from the PHP manual - json_decode — Decodes a JSON string

//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()

<?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

to get error.

Sign up to request clarification or add additional context in comments.

2 Comments

I think that the problem is that $data = $this->_request->getPost(); returns an array not string and thats why Zend_Json::decode($data, true); throws an error :(
@gruber True, but $this_request->getPost('data') should have a json string. or you could try $this->_request->getRawBody(), which returns the raw post data.
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
});

4 Comments

What is PHP receiving? Can you post a print_r($data); and show us the results?
Your edit is, once again, wrong. Your edited AJAX call does not look like mine above. Notice I don't have quotes around the whole data section and that there isn't a single apostrophe in my data either.
would it be ok ? $.ajax({ url: 'testURL', type: 'POST', dataType: 'json', data: {"userId": "test1", "imageUrl": "test2", "message": "test3" }, success: callBack });
Yes. At this point, however, it's post data and does not to be json_decode()d. You can just access it using $data as an array.
0

I suggest the following:

  1. Remove dataType: 'json' from the AJAX request.
  2. In the action, use return $this->_helper->json($responseArray);. No need to change layout or anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.