繁体   English   中英

使用ajax将多维数组数据发送到PHP脚本

[英]Sending Multidimentional array data, with ajax, to a php script

我有两个简单的脚本。 一个具有多维数组和服务器端php脚本的客户端jquery。 在PHP $数据保持为空。

jQuery的

console.log("IN JQUERY");
console.log(inps);

$.ajax({
  type:           'post',
  cache:          false,
  url:            './gen.php',
  data:           inps,
  success: function(response){
    console.log("RESPONSE");
    console.log(response);
  }
});

gen.php

<?php
$data = file_get_contents('php://input');
$data = json_decode($data, true);
print_r($data);
?>

火狐控制台输出

>POST ..././gen.php [HTTP/1.1 200 OK 1ms]
>"IN JQUERY" 
>{isbranch: "1", ismanager: "0", isdept: "1"} 
>"RESPONSE" 
>""

有没有一种方法可以使用ajax将多维数组发送到php而不拆分数组?

您应该在发送数据之前使用JSON.stringify对数据进行编码,也最好向其添加正确的contentType:

$.ajax({
  type:           'post',
  cache:          false,
  url:            '/gen.php',
  data:           JSON.stringify(inps),
  contentType:    'application/json',
  success: function(response){
    console.log("RESPONSE");
    console.log(response);
  }
});

键值对应该已经在您的$_POST存在

$isbranch = $_POST['isbranch'];
$ismanager = $_POST['ismanager'];
$isdept = $_POST['isdept'];

暂无
暂无

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

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