我想创建一个关联数组与记录ID作为键和顺序作为值。然后我想通过ajax传递给PHP,我将通过数组来更新记录。但它不工作我似乎在json_decode($ _ REQUEST ['orderArray'],true);通过jquery ajax传递对象数组到php不工作
的哪些错误的代码:
的jquery: orderArray { “4b0df1da-8b2d-7776-0026-52d0b3cefbfa” :: “3”,“161699ae-
//Make project task table rows sortable
$('#Task_table tbody').sortable({
items: "tr:not(.disable_sort)",//disable sortable on header row
helper: fixHelperModified, //call helper function
update: function(event, ui) {
var order = {};//create object
$('#Task_table tr').each(function(){//loop through rows
var id = $(this).children('td:first-child').find(".order_number").attr("rel");
var order_number = $(this).children('td:first-child').find(".order_number").val();
//fill object array with keys(task->id) and values (task->order_number)
order[id] = order_number;
});
//convert array to json
var jsonArray = JSON.stringify(order);
//prepare POST data
var dataString = { 'orderArray':jsonArray };
$.ajax({
type: "POST",
url: "index.php?module=Project&action=update_order",
data: dataString,
success: function() {
// location.reload();
}
});
}
});
此通孔柱发送6db0-43d6-e85b-52ca07767b0f “:” 1" , “8da4cfc3-b56d-12DA-e34c-52d09ed0b310”: “2”}
在PHP:
//updates the order of the tasks
function action_update_order(){
//create object/array from json data
$orderArray = json_decode($_REQUEST['orderArray'], true);
var_dump($orderArray);
foreach($orderArray as $id => $order_number){
$GLOBALS['log']->fatal('order: '.$order_number[$id]);
$task = new ProjectTask();
$task->retrieve($id);
$task->order_number = $order_number;
$task->save();
}
}
正如我所说,我似乎无法通过jasondecode的结果。也很难作为它的ajax进行调试。
有没有听说过[Firebug](https://getfirebug.com/)?将向您展示正在发布的内容以及回复内容。 – Machavity
使用萤火虫,我知道什么被张贴:它的这个orderArray {“4b0df1da-8b2d-7776-0026-52d0b3cefbfa”:“3”,“161699ae-6db0-43d6-e85b-52ca07767b0f”:“1”,“8da4cfc3-b56d -12da-e34c-52d09ed0b310“:”2“}在respoce中有很多东西,但不能看到任何var_dump。 – user794846
使用Console.log()可以打印到开发者控制台,如Firebug。您可以打印对象,数组和基本数据类型。 – James