views:

91

answers:

5

I need to pass a JSON object from $.post() jQuery function to PHP page.
I tried this plugin jquery-json, but don't work:

//include jquery.js and jquery.json.js

$(document).ready(function(){ 
   var data = new Object(); 
   data.hello = "Hello"; 
   data.world = "World"; 
   var datajson = $.toJSON(data); 
   $.post('file.php', {data: datajson}, function(res){ 
   alert(res);
   }); 
});

And file.php has this simple line:

<?
var_dump(json_decode($_REQUEST['data'], true));
?>

What's the problem ?

+2  A: 

A few things:

you don't declare a datajson variable, should it be


$.post('file.php', {data: dataString}, function(res){ 
   alert(res);
}); 

instead?

Looks like you also have a type in your PHP: son_decode needs to be json_decode.

mmattax
yes i wrong to write...but dont' work.
enfix
A: 

I think this function is misspelled son_decode

It should be json_decode reference here

Shuriken
yes i wrong to write...but dont' work.
enfix
A: 

Firebug parameters are ok, but the alert return NULL.
PHP problem ?

enfix
A: 

Just check that your PHP is >= 5.2.0 If it's not you can include a compat library, similar to one at http://pear.php.net/pepr/pepr-proposal-show.php?id=198

Danila V.
A: 

Check withphpinfo() that php-json is in fact available. In some installations, it's not.

Konrad Neuwirth