首页 > 解决方案 > ajax laravel更新功能中的列不能为空

问题描述

大家好,我正在将我的更新值从 ajax 发送到我的更新函数。我收到错误,因为“列不能为空”

这是我发送 json 数据的输入:

        <input type="text" id="jsonData" name="jsonData">

这是我的ajax表单:

function saveEditQtypeFile(edit_qtype_id)
    {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

       chk_EnumValuesValidation = chkEnumValuesValidation(isSoltion, stepCount);
       if(!chk_EnumValuesValidation)
       {
           return false;
       }
        else
        {
            // Function to push "MainArray" in current Solution
            pushVarMainArrayInThisSolution(isSoltion, var_main_arr.var_arr_values);

            arr                = ar;
            var edit_qtype_id = $('#edit_qtype_id').val();
            var qtype_name = $('#qtype_name').val();
            var subject_list   = $('#qtype_subject_id').val();
            var ddl_topic_type = $('#qtype_topic_id').val();
            var qtype_option = $('#qtype_option').val();
            var jasondata = $('#jsonData').val();
            var sort_order = $('#sort_order').val();

            var sendInfo       = {
                'edit_qtype_id':edit_qtype_id,
                'arr':arr,
                'saveEditQtypeFile':1,
                'qtype_name':qtype_name,
                'qtype_subject_id':subject_list,
                'qtype_topic_id':ddl_topic_type,
                'qtype_option':qtype_option,
                'qtype_json':jasondata,
                'sort_order':sort_order
            };

            console.log('json',jasondata);
            //return false;

              //var loadQtypeInfo = JSON.stringify(sendInfo);
              $.ajax({
                url: "/eqtype-editor/update",
                type: "POST",
                data :sendInfo,
                contentType: "application/x-www-form-urlencoded",

                success: function(response)
                {
                    alert('Your file is updated!');
                    window.location.href ="/eqtype-editor";

                },
                error: function (request, status, error)
                {
                    alert('problem with updating record!!!');
                },
                complete: function()
                {}
            });
        }
    }

这是我的控制器:

 public function update(Request $request, Qtype_editor $qtype_editor)
{

    $qtype_editor = Qtype_editor::findOrFail($request->edit_qtype_id);
    $qtype_editor->qtype_name = $request->input('qtype_name');
    $qtype_editor->qtype_subject_id = $request->input('qtype_subject_id');
    $qtype_editor->qtype_topic_id = $request->input('qtype_topic_id');
    $qtype_editor->qtype_option = $request->input('qtype_option');
    $qtype_editor->qtype_json = json_decode($request->input('jsonData'));
    $qtype_editor->sort_order = $request->input('sort_order');

    $qtype_editor->save();

    return redirect()->route('eqtype-editor.index');
}

当我控制台时从 ajax 获取我的 json 数据..我收到错误,因为 qtype_json 不能为空。

谁能帮我在我想念它的地方。

提前致谢。

标签: ajaxlaravel

解决方案


您为数据使用了错误的密钥。

更改控制器中的行:

$qtype_editor->qtype_json = json_decode($request->input('jsonData'));

$qtype_editor->qtype_json = json_decode($request->input('qtype_json'));

它会起作用。


推荐阅读