如果可以的话,我会建议改变你正在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";
?>