JSON对象通过jQuery发布到php

编程入门 行业动态 更新时间:2024-10-23 08:35:35
本文介绍了JSON对象通过jQuery发布到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道,那里有很多问题,但没有一个问题适合我。

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

本文发布于:2023-10-11 23:21:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   JSON   php   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!