通过Ajax传递数组会添加一个“未定义”行

时间:2015-02-22 13:33:34

标签: javascript ajax

我目前正在尝试首次尝试AJAX。

所以在我的JS中,我已经定义了一个名为currentOrder的对象,它具有这些属性(取自Chrome的控制台)。

client: "1"
comments: "qsdf"
date: "22-02-2015"
products: Array[3]
  1: Object
    amount: 1
    pid: "1"
    productName: "Test Paint
  2: Object
    amount: 1
    pid: "2"
    productName: "Groene lijnmarkering"

但是,当我通过AJAX传递数组时,会添加一些产品[]:undefined。这是我检查post方法得到的结果:

products[]:undefined
products[1][productName]:Test Paint
products[1][amount]:1
products[1][pid]:1
products[2][productName]:Groene lijnmarkering
products[2][amount]:1
products[2][pid]:2
client:1
date:22-02-2015
comments:qsdf

添加products[]:undefined的地方来自哪里?

这是我的JS功能:

      var oTable = $('#datatable-1').dataTable();
      var pTable = $('#datatable-2').dataTable();

      var currentOrder = {};
      currentOrder.products = [];

      $('.product-row').dblclick(function() {

        var clickedProduct = {};

        clickedProduct.ID = $(this).children().eq(0).text();

        if (currentOrder.products[clickedProduct.ID]) {
          currentOrder.products[clickedProduct.ID].amount++;
          var elementID = 'product-' + clickedProduct.ID;
          document.getElementById(elementID).value = currentOrder.products[clickedProduct.ID].amount;
        }
        else {
          var newOrderLine = {};
          newOrderLine.productName = $(this).children().eq(2).text();
          newOrderLine.amount = 1;
          newOrderLine.pid = clickedProduct.ID;
          currentOrder.products[clickedProduct.ID] = (newOrderLine);
          //currentOrder.products[clickedProduct.ID].productName = $(this).children().eq(2).text()
          //currentOrder.products[clickedProduct.ID].amount = 1;
          oTable.fnAddData( [
            $(this).children().eq(1).text(),
            '<input id="product-' + clickedProduct.ID + '" type="text" value="1" size="3">',]
          );
        }

        console.log(currentOrder);
      });

      $('#products-linemarkings').click(function() {

        var selectDropdown = document.getElementById("edit-client");
        var currentClient = selectDropdown.options[selectDropdown.selectedIndex].value;

        var e = document.getElementById("new-order-date-datepicker-popup-0");
        var curDate = e.value;

        var c = document.getElementById("edit-comments").value;

        currentOrder.client = currentClient;
        currentOrder.date = curDate;
        currentOrder.comments = c;
        $.post( "http://localhost/expoline/test", currentOrder, function( data ) {
          alert("succes");
        });
      });

有什么想法吗?

0 个答案:

没有答案