0

这是生成数组并将其发布到我的后端 API 的代码。在后端,我将 $_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;
4

1 回答 1

0

将 JavaScript 更改为以下工作:

$('#find_carriers').click(function(){

    var data = new Object();

    data.general = {
        'shipper': $('#shipper_zip').val(),
        'consignee': $('#consignee_zip').val(),
        'shipment_type': $('#direction').val(),
        'total_weight': $('#total_weight').text()
    };

    data.accessorials = [];

    $('#accessorials').find('input:checkbox:checked').each(function(acc_index){


        data.accessorials.push($(this).val());

    });

    alert('hello')

    data.units = [];
    data.products = [];

    $('#units').find('table.unit').each(function(unit_index){

        data.units.push({
            '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.products.push({
                '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
    (
        [general] => Array
            (
                [shipper] => 
                [consignee] => 
                [shipment_type] => Outbound/Prepaid
                [total_weight] => 2
            )

        [accessorials] => Array
            (
                [0] => 17
                [1] => 19
            )

        [units] => Array
            (
                [0] => Array
                    (
                        [num_of] => 1
                        [type] => Bobs
                        [weight] => 1
                    )

                [1] => Array
                    (
                        [num_of] => 1
                        [type] => Bobs
                        [weight] => 1
                    )

            )

        [products] => Array
            (
                [0] => Array
                    (
                        [pieces] => 1
                        [weight] => 1
                        [class] => 
                    )

                [1] => Array
                    (
                        [pieces] => 1
                        [weight] => 1
                        [class] => 
                    )

                [2] => Array
                    (
                        [pieces] => 1
                        [weight] => 1
                        [class] => 
                    )

            )

    )

[message] => Array
    (
    )

)
于 2013-02-12T21:05:52.020 回答