簡體   English   中英

通過jQuery AJAX調用獲取數組不起作用

[英]Get array through an jQuery AJAX call doesn't work

我有以下代碼:

 var data = $(this).sortable('serialize');
$.ajax({
                    data: {order: data, actionFor: 'main_articles'},
                    type: 'POST',
                    url: 'updateDisplayOrder.php',
                    success: function (msg) { //re-number the rows from 1 to n
                       //code goes here
                    },
                    error: function () {
                        alert("An error occurred");
                    });

和PHP:

    require_once('../lib/common/db_connect.php');
    $ids = array();
    $actionFor = $_POST['actionFor'];
    foreach ($_POST['order'] as $value) //error here {
    //more code goes here
    }

問題是我在foreach行上收到此錯誤:

警告:為foreach()提供了無效的參數

我注意到,如果我更改此行:

 data: {order: data, actionFor: 'main_articles'}, 

data:data

在PHP中:

foreach ($_POST['order'] as $value) //error here {

foreach ($_POST['item'] as $value)

效果很好。 為什么? 我該如何欺騙它? 謝謝!

$_POST['order']包含字符串,而不是數組。 您需要首先將字符串解析為數組。 嘗試這個:

$order = array();
$stg1 = explode("&", $_POST['order']);
$i = 0;
foreach($stg1 as $keyval) {
    list($key, $val) = explode("=", $keyval);
    $order[$i++] = $val;
}

然后,您可以根據需要執行foreach($order as $value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM