php获取ajax数组,从AJAX获取PHP中的JSON数组请求

如果可以的话,我会建议改变你正在jQuery的请求发送到以下方式:

var h1 = []

h2 = []

h3 = [],

layout = $("input[type=radio][name='layout_option']:checked").val();

$("ul.widget-order[name='1'] li").each(function() { h1.push($(this).attr('id')); });

$("ul.widget-order[name='2'] li").each(function() { h2.push($(this).attr('id')); });

$("ul.widget-order[name='3'] li").each(function() { h3.push($(this).attr('id')); });

var sendData = JSON.stringify({

ids1: " " + h1 + "",

ids2: " " + h2 + "",

ids3: " " + h3 + ""

});

var sPayload = "data=" + encodeURIComponent(sendData) + "&layout=" + encodeURIComponent(layout);

$.ajax({

type: "POST",

url: "_backend/account/updateWidgets.php",

data: sPayload,

success: function(data){

$("#post_reply").html(data);

console.log({ data: sendData });

}

});

有在您发布的代码中发现了一些语法错误,这些错误在我的答案中包含的代码中已得到纠正,但最重要的是,我已将您的数据修改为全部编码。看起来你正在试图将JSON对象与编码为application/x-www-form-urlencoded的标准数据结合起来。我已经在上面的代码中对此进行了标准化,因此所有数据均发布为application/x-www-form-urlencoded。

在你的PHP,你就可以访问此为:

// First, let's get the layout because that's the easiest

$layout = (isset($_POST['layout']) ? $_POST['layout'] : NULL);

// As a note, the isset(...) with ternary operator is just my preferred method for checking missing data when extracting that data from the PHP super-globals.

// Now we'll need to get the JSON portion.

$jData = $_POST['data'];

$data = json_decode($jData, $assoc=true);

echo "ids1 is " . $data['ids1'] . "\n
\n";

echo "ids2 is " . $data['ids2'] . "\n
\n";

echo "ids3 is " . $data['ids3'] . "\n
\n";

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值