簡體   English   中英

動態數組有問題

[英]Having issues with dynamic array

這是生成數組並將其發布到我的后端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;

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

)

暫無
暫無

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

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