简体   繁体   English

动态数组有问题

[英]Having issues with dynamic array

This is the code that generates the array and POSTs it to my backend API. 这是生成数组并将其发布到我的后端API的代码。 On the backend I store $_POST in $page['body'] and print_r($page) in an attempt to test to make sure the data I'm sending is showing up correctly and formatted right. 在后端,我将$ _POST存储在$ page ['body']和print_r($ page)中,以尝试进行测试以确保所发送的数据正确显示并正确格式化。 Below is the result. 结果如下。 JavaScript and I are not friends, so any pointers/tips for fixing the array are much appreciated. 我和JavaScript不是朋友,因此非常感谢您修复数组的任何指针/技巧。

if you look in the response in the [body] index, the data being passed by JavaScript is showing up there as being 'undefined'. 如果您在[body]索引中查看响应,则JavaScript传递的数据在那里显示为“未定义”。 I'm not sure what is causing this. 我不确定是什么原因造成的。

Request 请求

$('#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;

    });

Response 响应

Array
(
  [user] => Array
    (
        [id] => 33
        [name] => user
        [authenticated] => 1
        [level] => user
    )

[title] => 
[body] => Array
    (
        [undefined] => 
    )

[message] => Array
    (
    )

)

PHP 的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 that Handles dumping 处理转储的PHP

    case 'text':

    print_r($page);

break;

Changing the JavaScript to the following works: 将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;

});

result 结果

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
    (
    )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM