これは、配列を生成してバックエンド API に POST するコードです。バックエンドで $_POST を $page['body'] と print_r($page) に保存して、送信しているデータが正しく表示され、正しくフォーマットされていることをテストします。以下は結果です。JavaScript と私は友達ではないので、配列を修正するためのポインタ/ヒントは大歓迎です。
[body] インデックスの応答を見ると、JavaScript によって渡されたデータが「未定義」として表示されています。何が原因なのかわかりません。
リクエスト
$('#find_carriers').click(function(){
var data = new Array();
data.push( { 'general': {
'shipper': $('#shipper_zip').val(),
'consignee': $('#consignee_zip').val(),
'shipment_type': $('#direction').val(),
'total_weight': $('#total_weight').val()
}
}
);
$('#accessorials').find('input:checkbox:checked').each(function(acc_index){
data.push( {'accessorials': {acc_index : $(this).val() } } );
});
$('#units').find('table.unit').each(function(unit_index){
data.push( {'units': { unit_index : {
'num_of': $(this).find('.unit_num_of').text(),
'type': $(this).find('.unit_type').text(),
'weight': $(this).find('.unit_weight').text()
}
}
}
);
$(this).find('tbody.products tr').each(function(product_index){
data.push( {'products': { product_index : {
'pieces': $(this).find('td.pieces').text(),
'weight': $(this).find('td.weight').text(),
'class': $(this).find('td.class').text()
}
}
}
);
});
});
$.post('index.php?p=api&r=text&c=ctsi&m=lcc', data, function(resp){
alert(resp);
});
return false;
});
応答
Array
(
[user] => Array
(
[id] => 33
[name] => user
[authenticated] => 1
[level] => user
)
[title] =>
[body] => Array
(
[undefined] =>
)
[message] => Array
(
)
)
PHP
public function lcc($data) {
global $page;
if($page['user']['level'] != "user") {
$this->errorState = 1;
$this->errorMessage = "Access denied.";
return FALSE;
}
$page['body'] = $_POST;
}
ダンプを処理する PHP
case 'text':
print_r($page);
break;