简体   繁体   English

如何将javascript对象数组传递给php?

[英]how to pass array of javascript objects to php?

I have a bunch of values that need to be passed to the server, and I can't seem to get the basic functions like JSON decode to work. 我有一堆需要传递给服务器的值,而且似乎无法使JSON解码等基本功能正常工作。 Something is off here, I'll appreciate someone to look at it 这里有些事,我会很感激有人看的

I have a dynamically generated table with editable values that I grab when user clicks Add button, push it into an array and send it off to the server with ajax call. 我有一个动态生成的表,该表具有可编辑的值,当用户单击“添加”按钮时,该表会被捕获,将其压入数组,然后通过ajax调用发送到服务器。 Here's sample data sent over that I copied from console.log(data); 这是从console.log(data)复制过来的示例数据。 line. 线。

var data = params + "&Details="+JSON.stringify(Details);
console.log(data);     // &Id[]=1566&aId[]=1567&Details=[["1566","First File","sdf.pdf","general","file","",""],["1567","2nd file","test.png","image","file","",""]] 

$.ajax({
    type: 'POST',
    url: editUrl,
    dataType: 'json',
    data: data,
    success: function() { console.log('success'); }
    error: function() { console.log('error'); }
});

On the server side I get the data but I can't parse it into usable format 在服务器端,我得到了数据,但无法将其解析为可用格式

$ids = ($_POST['Id']);    // this is a valid array
$details = ($_POST['Details']);     // [[ 1566 , First File ,  , general , file ,  ,  ],[ 1567 , 2nd file ,  , image , file ,  ,  ]]
$details = json_decode($details);   // getting JSON_ERROR_CTRL_CHAR error and null is returned

any idea what's wrong in here? 知道这里有什么问题吗?

You need to encode the JSON properly: 您需要正确编码JSON:

var data = params + "&Details="+encodeURIComponent(JSON.stringify(Details));

However, it would be better, IMHO, to make data an object rather than a string. 但是,恕我直言,使data成为对象而不是字符串会更好。

var data = {
    Id: [ 1566, 1567],
    Details: JSON.stringify(Details)
};

You'll need to change how you create params so it produces an object as well. 您将需要更改创建params以便它也可以生成对象。 You can then use $.extend() to merge them: 然后,您可以使用$.extend()合并它们:

var data = $.extend({}, params, { Details: JSON.stringify(Details) });

Your ajax parameter missing a comma after success function, and try to define success function callback from server as data and then use json_encode for your array data in php server. 您的ajax参数在成功函数后缺少comma ,并尝试将服务器的成功函数回调定义为数据,然后将json_encode用于php服务器中的数组数据。

$.ajax({
   type: 'POST',
   url: editUrl,
   dataType: 'json',
   data: data,
   success: function(data) { console.log(data); },
   error: function() { console.log('error'); }
});

Not sure how you are building out params variable or why. 不确定如何构建params变量或为什么。 But if it's just to send via ajax you don't have to. 但是,如果只是通过ajax发送,则不必这样做。 The data parameter of ajax will do this for you. ajax的data参数将为您完成此操作。 Give it an object with your name/value pairs that you want to send. 给它一个带有您要发送的名称/值对的对象。

$.ajax({
  type: 'POST',
  url: editUrl,
  dataType: 'json',
  data: { 
    Id: [2566],
    aId: [1567],
    Details: Details,
    Other: 'Hi',
    AnotherId: 1234
  }
  success: function() { console.log('success'); }
  error: function() { console.log('error'); }
});

On the server take a look at all the $_POST variable to get an idea of what was sent and the format 在服务器上,查看所有$ _POST变量,以了解发送的内容和格式

<?php
  echo '<pre>'
  print_r($_POST);
?>

尝试json_decode(stripslashes($details))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM