开发者

How to send javascript key-value pair to server?

开发者 https://www.devze.com 2023-01-10 23:40 出处:网络
I have a key value pair which i want to send to server. e.g: var obj = {\'item1\': true, \'item2\': false,开发者_运维百科 ........};

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?


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)});


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));
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消