以JSON和json_de code在PHP和JavaScript [英] To JSON and json_decode in PHP and JavaScript

查看:578
本文介绍了以JSON和json_de code在PHP和JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个JavaScript对象传递给PHP脚本通过 jquery.ajax(),基本上是:

  VAR bigArray =新的对象();
//核桃
//开始循环
bigArray [X] = {名称:exname,ID:exID,顺序:即设置:setBox,INC:incBox,例如:exampleBox,天:我};
 

因此​​,它是pretty的这些对象的多阵列。

  VAR anotherTest = $ .toJSON(bigArray);
    VAR ajxFile =routineajax.php;
    $阿贾克斯({
        键入:POST,
        过程数据:假的,
        网址:ajxFile,
        数据:anotherTest,
        成功:功能(数据){
            $('#结果)HTML(数据)。
            ('进行加载。')警告;
        }
    });
});
 

在PHP端脚本

 的print_r($ _ POST);
 $ PARAMS = json_de code($ _ POST);
 的print_r($ params)方法
 

AJAX调用正在经历,我可以看到在萤火虫,但的print_r($ _ POST)将返回一个空数组。而如果我把它改为 $ _ GET 在两个 $。阿贾克斯函数和PHP脚本,它的工作原理。我的主要问题是我收到此错误信息:

  

警告:json_de code()预计参数1为字符串,在

定的数组

我该如何解决这个问题?


添加此片段到PHP文件后

  $数据=的file_get_contents('php的://输入');
  后续代码var_dump($的数据);
  后续代码var_dump(json_de code($数据));
 

我得到这个输出

<$p$p><$c$c>string'{"0"{"name":"Decline`Abs","id":"54","order":0,"set":"","inc":"","example":"","day":1}}' (长度= 87)`   对象(stdClass的)[2]       公众'0'=&GT;         对象(stdClass的)[4]           公众的名字'=&GT;字符串'绝对下降(长度= 11)           公众'身份证'=&GT;字符串'54'(长度= 2)           公众秩序=&GT; INT 0           公众'设置'=&GT;字符串''(长度= 0)           公众INC​​'=&GT;字符串''(长度= 0)           公众例如'=&GT;字符串''(长度= 0)           大众日'=&GT; INT 1

所以,至少它的经历,我不知道如何访问它虽然,一步步朝着正确的方向!

解决方案

我认为这个问题是一个通常POST数据发送连接codeD为键=值放大器;键2 =值,和你发送它作为JSON。尝试访问原始数据后,如下所示:

  $数据=的file_get_contents('php的://输入');
后续代码var_dump($的数据);
后续代码var_dump(json_de code($数据));
 

,看看是否可行。如果没有,请在你的问题是什么返回,如果有的话。


基于下面的注释和补充OP。

做了的var_dump $的数据复制粘贴正确的?我想问的原因是这样的:字符串{0{姓名不看我的权利。这不是有效的JSON或正确连接codeD POST字符串。这可能是某些字符连接了codeD,当你复制和粘贴。

无论哪种方式,你现在得到你需要的结果。该 stdClass的只是,它把数据放入,可以使用正常的对象语法访问一个空容器中。在这种情况下,你所要做的 $数据 - &GT; {0} - &GT;名称我想正因为如此 0 。如果你这样做 $数据= json_de code($的数据,真实)这将是一个关联数组,你可以访问POST'ed数据 $数据[0] ['名称']

如果你想保持探索这个,它可能是有帮助的,以显示做的结果 window.console.dir(数据)你做的Ajax请求权之前,并确保当你的var_dump(数据),查看网页复制和粘贴的来源。 window.console.dir(数据)将显示数据的在Firebug窗口的属性(您使用的是萤火虫,对?)。它也可以在Chrome浏览器的调试器,也许其他人。就像我说的,虽然,它看起来像你可能会得到你需要的了,所以调查是没有必要的。

I'm trying to pass a JavaScript object to a PHP script through jquery.ajax(), basically:

var bigArray = new Object();
//Dode
//Start loop
bigArray[x] = {name: exname, id: exID, order:e, set: setBox, inc: incBox, example: exampleBox, day: i};

So it's pretty much an array of these objects.

    var anotherTest = $.toJSON(bigArray);
    var ajxFile = "routineajax.php";
    $.ajax({
        type: 'POST',
        processData: false,
        url: ajxFile,
        data: anotherTest,
        success: function(data) {
            $('#result').html(data);
            alert('Load was performed.');
        }
    });
});

The PHP side script

 print_r($_POST);
 $params = json_decode($_POST);
 print_r($params)

The Ajax call is going through, and I can see in Firebug, but print_r($_POST) is returning an empty array. While if I change it to $_GET in both the $.ajax function and PHP script it works. My main problem is I'm getting this error message:

Warning: json_decode() expects parameter 1 to be string, array given in

How do I fix this problem?


After adding this snippet to the PHP file

  $data = file_get_contents('php://input');
  var_dump($data);
  var_dump(json_decode($data));

I'm getting this output

string'{"0"{"name":"Decline`Abs","id":"54","order":0,"set":"","inc":"","example":"","day":1}}' (length=87)`
  object(stdClass)[2]
      public '0' =>
        object(stdClass)[4]
          public 'name' => string 'Decline Abs' (length=11)
          public 'id' => string '54' (length=2)
          public 'order' => int 0
          public 'set' => string '' (length=0)
          public 'inc' => string '' (length=0)
          public 'example' => string '' (length=0)
          public 'day' => int 1

So at least it's going through, I'm not sure how to access it though, a step in the right direction!

解决方案

I think the problem is that normally POST data is sent encoded as key=value&key2=value2, and you're sending it as JSON. Try accessing the raw post data as follows:

$data = file_get_contents('php://input');
var_dump($data);
var_dump(json_decode($data));

and see if that works. If not, please post in your question what it returns, if anything.


Based on the comment below and additions to the OP.

Did the var_dump of $data copy-paste correctly? The reason I ask is that this: string'{"0"{"name" does not look right to me. That isn't valid JSON or a properly encoded POST string. It might be that some of the characters got encoded when you copied and pasted.

Either way, you're now getting the result you need. The stdClass is just a blank container that it puts the data into, which you can access using the normal object syntax. In this case, you'd have to do $data->{0}->name I think, because of that 0. If you do $data = json_decode($data, true) it will be an associative array, and you can access the POST'ed data as $data[0]['name'].

If you want to keep exploring this, it might be helpful to show the results of doing window.console.dir(data) right before you do the ajax request, and make sure when you var_dump(data), you view the source of the page to copy and paste. window.console.dir(data) will show data's properties in the Firebug window (you are using Firebug, right?). It also works in Chrome's debugger, and maybe others as well. Like I said though, it looks like you're probably getting what you need already, so investigating isn't necessary.

这篇关于以JSON和json_de code在PHP和JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆