views:

38

answers:

2

I have a key value pair which i want to send to server. e.g: var obj = {'item1': true, 'item2': false, ........};

I want to send this information to server by ajax call. But at server side i am unable to get individual value. At server side i am getting "object" as string. I am using jQuery for making an ajax call.

Can anyone please give any idea how to do it?

+1  A: 
var a = $.JSON.encode(obj);
$.post("test.php", {data:a});

Use a JSON decoder to convert the string in $_POST["data"] to an associate array at the server side.

<?php
  $json = $_POST["data"]
  var_dump(json_decode($json));
?>
Amarghosh
$.JSON.encode(obj) can be used by using any plugins?
Dev
A: 

I got answer to my question. This can be done by using:

var obj = {'item1': true, 'item2': false, ........};
$.post("test.php", {data: JSON.stringify(obj)});
Dev