php获取ajax数组,从AJAX获取PHP中的JSON数组请求
如果可以的話,我會建議改變你正在jQuery的請求發(fā)送到以下方式:
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 });
}
});
有在您發(fā)布的代碼中發(fā)現(xiàn)了一些語法錯誤,這些錯誤在我的答案中包含的代碼中已得到糾正,但最重要的是,我已將您的數(shù)據(jù)修改為全部編碼。看起來你正在試圖將JSON對象與編碼為application/x-www-form-urlencoded的標準數(shù)據(jù)結(jié)合起來。我已經(jīng)在上面的代碼中對此進行了標準化,因此所有數(shù)據(jù)均發(fā)布為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";
?>
總結(jié)
以上是生活随笔為你收集整理的php获取ajax数组,从AJAX获取PHP中的JSON数组请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql not in优化_实践中如何
- 下一篇: websocket中发生数据丢失_获取使