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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!