我知道,那里有很多问题,但没有一个问题适合我。
I know, there are plenty of questions out there, but none of them worked for me.
我在javascript中使用普通的javascript对象构建一个数组,并通过 jquery.post 将其发送到服务器。但是在服务器上,我无法使用 php $ obj->值访问数据。我试过 json_decode / encode 等等。
I build an array with normal javascript objects in javascript and sent it via jquery.post to the server. However on the server, I can't access the data using php $obj->value . I tried json_decode/encode and so on.
这就是 console.log(数据)在发送给服务器之前给我。
This is what console.log(data) gives me, before sending it to the server.
在php部分我只做这个:
Than on the php part I only do this:
$data= $_POST['data']; print_r($data);print_r的输出为:
The output of print_r is:
这就是我的Jquery帖子看起来像:
And thats how my Jquery post looks like:
$.post("programm_eintragen.php",{ data: data, }).success( function(data){ //success }).error( function(){ console.log("Error post ajax " ); },'json');有人可以告诉我:
如何正确访问php网站上的对象属性?
我还得到试图访问非对象.. .. 或php将json对象解释为字符串 data [0] 返回我 [。
I also get tried to access non object .... or php interprets the json object as a string an data[0] returns me [.
我想,我可以这样做:
$data[0]->uebungen[0]我只是在做什么愚蠢而且缺少一些东西?
为什么整个json向php发送这样的问题呢?
推荐答案在JavaScript中,您实际上并没有发送JSON编码的字符串,而只是发送表单数据。要实际发送JSON字符串,您需要将其(对象)转换为字符串。
In JavaScript, your're not actually sending a JSON encoded string, your are just sending form data. To actually send a JSON string, you need to convert it (the object) to a string.
$.post("programm_eintragen.php",{ data: JSON.stringify(data), });在接收方(php脚本),您将拥有一个JSON字符串。你可以解码它。
On the receiving side (php script) you will have a JSON string. You can decode it.
$data = json_decode($_POST['data'], true); var_dump($data[0]['uebungen'][0]);但是,这些步骤不是必需的。只需直接访问数组即可避免所有json_encoding。对于此示例,请忽略上述javascript,并且不要更改代码中的任何内容。
However, these steps are not necessary. All the json_encoding can be avoided by just accessing the array directly. For this example, ignore the above javascript, and don't change anything in your code.
$data = $_POST['data']; var_dump($data[0]['uebungen'][0]);示例
更多推荐
JSON对象通过jQuery发布到php
发布评论