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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵