dpg76975 2014-10-27 20:04
浏览 75
已采纳

序列化后数据表格到laravel控制器以进行保存

I have a datatables with some inputs in certain columns that are editable. I want to save the edited values in db.

This is my datatables:

enter image description here

The serialized datatables from values looks like this (if you print_r($_POST) for debugging):

enter image description here

javascript - I post the datatables form via ajax request to my laravel api controller method like this:

// Serialize the datatable table into post string
var poDetailUpdates = create_po_details_table.$('input').serialize();

// Call po api to save changes
$.ajax({
    type: "POST",
    data: poDetailUpdates,
    url: '/api/po/update-create-lines/',
    complete: function(response) {
        if (response.status === 200) {
            alert(response.responseText); // success
        } else {
            alert(response.responseText); // error
        }
    }
});

php - this is how I handle my form post in laravel:

public function postUpdateCreateLines() {
    DB::beginTransaction();
    try {
        foreach ($_POST as $column_name => $values) {
            foreach ($values as $line_id => $new_value) {
                DB::table('purchase_order_details')
                    ->where('id', '=', $line_id)
                    ->update(array($column_name => $new_value));
            }
        }
        DB::commit();
        Response::make('Purchase order details updated', 200);
    } catch (Exception $ex) {
        DB::rollback();
        Response::make('Purchase order details not updated - '. $ex->getMessage(), 500);
    }
}

When the code runs, I get an empty alert() in javascript once the ajax request completes.

I thought the issue might be with the DB transaction related code, so I tried commenting out the following:

// DB::beginTransaction();
// DB::commit();
// DB::rollback();

But this also yields the same result (empty alert). Any idea what might be wrong? how to debug this? Something must be going wrong somewhere...

  • 写回答

1条回答 默认 最新

  • doutan3371 2014-10-27 22:08
    关注

    Your postUpdateCreateLines() method is not actually returning anything; you are making a response, but you need to return it:

    return Response::make('Purchase order details updated', 200);
    

    However, there are some other improvements you could make. It is best to return a JSON response to your $.ajax call, and then rely on jQuery's ajax .done and .fail handlers to deal with the response appropriately (rather than using complete):

    return Response::json(
        ['status' => 'success', 
         'msg'    => 'Purchase order details updated']
         , 200) 
    

    ...and add the response dataType to your ajax call:

    dataType: "json"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗