Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
(
)
share|improve this question
is your JSON string in proper format? – shiplu.mokadd.im Feb 1 '12 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? – Carpetsmoker Feb 1 '12 at 21:31
please look at my edits – gruber Feb 1 '12 at 22:00
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. – Crashspeeder Feb 1 '12 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. – Crashspeeder Feb 1 '12 at 22:45

3 Answers

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.

share|improve this answer
1  
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 Feb 2 '12 at 9:10
@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. – RockyFord Feb 2 '12 at 15:39

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
});
share|improve this answer
didnt work :/ the same error – gruber Feb 1 '12 at 22:07
What is PHP receiving? Can you post a print_r($data); and show us the results? – Crashspeeder Feb 1 '12 at 22:14
look at my edits – gruber Feb 1 '12 at 22:32
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. – Crashspeeder Feb 1 '12 at 22:43
would it be ok ? $.ajax({ url: 'testURL', type: 'POST', dataType: 'json', data: {"userId": "test1", "imageUrl": "test2", "message": "test3" }, success: callBack }); – gruber Feb 1 '12 at 22:47
show 1 more comment

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.
share|improve this answer
1  
Any new Zend framework developer face the same problems :) – palAlaa May 23 '12 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.