2013-02-12 3 views
0

Это код, который генерирует массив и POST-файлы для моего внутреннего API. На бэкэнд я хранил $ _POST в $ page ['body'] и print_r ($ page), пытаясь проверить, чтобы данные, которые я отправляю, отображаются правильно и отформатированы правильно. Ниже приведен результат. JavaScript и я не друзья, поэтому любые указатели/советы по исправлению массива очень ценятся.Возникли проблемы с динамическим массивом

Если вы посмотрите в ответе в индексе [body], данные, передаваемые JavaScript, отображаются там как «undefined». Я не уверен, что вызывает это.

Запрос

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

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; 
+0

В чем проблема именно? –

+0

В чем конкретно проблема? –

+0

Извините, если вы посмотрите в разделе ответа, [body] => array ([undefined] =>), где хранятся опубликованные данные из JS. Массив, который я передаю, на самом деле происходит. Я не знаю, связано ли это с тем, что мой массив ошибочен или что-то еще. – DavidScherer

ответ

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

) 
+0

Я точно не знаю, почему это нужно было сделать именно так. Достаточно сказать, что JS заставляет меня хотеть найти ближайший мост с автострадой под ним и прыгать. – DavidScherer

Смежные вопросы