首页 > 解决方案 > 如何在 ajax 的下一个插入中修复“数组到字符串的转换”

问题描述

我有关于在表 2 上插入值的问题,ParentID 的插入值它给出了一个数组到字符串的转换错误。

场景:在第一个表上插入父项后,在成功函数中会插入子项。

目前我使用 Laravel 和 Ajax 作为我的后端和前端。

在我的前端:

var orderNumber;
var orders = [];
var menu;


$('#add_to_cart').on('click', function() {
  var customer_id = $('#hidden_customer_id').val();
  $('.tbody_noun_chaining_order').children('tr').each(function() {
    $row = $(this);
    if ($row.hasClass('condimentParent')) {

      // store a previous menu to the orders array if exists.
      if (menu !== undefined) {
        orders.push(menu);
      }
      menu = {
        'total': $row.find('.total').text(),
        'name': $row.find('.parent_item').text(),
        'customer_id': customer_id,
        'condiments': {
          'Item': [],
          'Qty': [],
          'Total': []
        }
      };

    } else if ($row.hasClass('editCondiments')) {
      // row is a condiment, append elements to the previous "menu" variable
      menu.condiments.Item.push($row.find('.child_item').text());
      menu.condiments.Qty.push($row.find('.condiments_order_quantity').text());
      menu.condiments.Total.push($row.find('.total').text());
    }
  });
  if (menu) {
    orders.push(menu);
  }
  storeOrder(orders);
});

function storeOrder(data) {
  var customer_id = $('#hidden_customer_id').val();
  for (var num in orders) {

    // simulate storing the order
    $.ajax('/insert_wish_list_menu_order', {
      type: 'POST',
      // as the call is asynchronous, make sure to provide all required reference data along with the call.
      context: orders[num].condiments,
      data: {
        // 'json': '{"orderNumber":' + (orderNumber++) + '}',
        'append_customer_noun_order_price': orders[num].total,
        'append_customer_noun_order': orders[num].name,
        'customer_id': customer_id
      },
      success: function(orderNumber) {
        if (orderNumber !== undefined) {

          $.ajax('/insert_wish_list_menu_belong_condiments', {
            context: orderNumber,
            type: 'POST',
            data: {
              'ParentId': orderNumber,
              'Item': this.Item,
              'Qty': this.Qty,
              'Total': this.Total
              // 'json': '{"condimentsCount": ' + this.Item.length + '}'
            },
            success: function(result) {
              //$('#result').append('Stored ' + result.condimentsCount + ' condiments for order ' + this + '<br />');
              console.log(result);
            }
          })

        }
      }
    })
  }
}

在我的后端:

public function insert_wish_list_menu_order(Request $request) {

    $customer_id = $request->get('customer_id');
    $append_customer_noun_order = $request->get('append_customer_noun_order');
    $append_customer_noun_order_price = $request->get('append_customer_noun_order_price');
    $now = new DateTime();


    DB::insert('INSERT INTO wish_list_menu_order (customer_id,wish_list_menu_name,wish_list_total_price,wish_created_at) 
    VALUES(?,?,?,?) ', [

        $customer_id,
        $append_customer_noun_order,
        $append_customer_noun_order_price,
        $now,
    ]);

    $last_id_insert = DB::select('SELECT LAST_INSERT_ID() as id');
    return response()->json($last_id_insert);   


} 


public function insert_wish_list_menu_belong_condiments(Request $request) {

    $Qty = $request->get('Qty');
    $Item = $request->get('Item');
    $Total = $request->get('Total');
    $ParentId = $request->get('ParentId');
    $now = new DateTime();

    if($Item)
    {
        for($count = 0; $count<count($Item); $count++)
        {

            DB::insert('INSERT INTO wish_list_menu_belong_condiments (wish_menu_id,belong_condi_name,belong_condi_qty,belong_condi_price,belong_condi_created) 
            VALUES(?,?,?,?,?) ',[

                 $ParentId,
                 $Item[$count],
                 $Qty[$count],
                 $Total[$count],
                 $now
            ]);

        }
    }

    return response()->json('Successfully Inserted');
} 

错误:

错误

参数

标签: jquerylaravel

解决方案


DB::select返回一个数组,因此错误。您需要返回第一个结果:

return response()->json($last_id_insert[0]); 

或者在 Javascript 端执行此操作。


推荐阅读