Laravel和Ajax更新不允许

问题描述 投票:1回答:2

我正在使用Ajax请求在Laravel中处理CRUD,它工作得很好,直到我偶然发现模型更新错误。当单击编辑按钮时,模态窗口将加载员工的所有数据,当我编辑任何内容时,Ajax将抛出一个错误,表示即使填充了两个字段也是如此。

这是控制台中抛出的错误:

{
  "readyState": 4,
  "responseText": "{\"email\":[\"The email field is requested.\"],\"fullname\":[\"The fullname field is requested.\"]}",
  "responseJSON": {
    "email": [
      "The email field is requested."
    ],
    "fullname": [
      "The fullname field is requested."
    ]
  },
  "status": 422,
  "statusText": "Unprocessable Entity"
}

这是Ajax函数:

/* Edit employee */
$(document).on('click', '#btn-edit', function(e) {
    e.preventDefault();
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'put',
        url: link + id,
        processData: false,
        contentType: false,
        // data: new FormData($("#form-employee")[0]),
        data: $("#form-employee").serializeArray(),
        success: function() {
            $('#btn-update').click();
            $('#employeeModal').modal('hide');
            swal(i18n.buttons.updated_msg, i18n.employees.updated_alert, "success");
        },
        error: function(data) {
            var errors = data.responseJSON;
            $('.form-group').removeClass('has-error');
            $('.alert ul').empty();
            $('.alert').show();
            $.each(errors, function(key, value) {
                $('.alert ul').append('<li>' + value + '</li>');
                $('#' + key).parent('.form-group').addClass('has-error');
            });
            console.log(data);
        }
    });
});

而Laravel验证:

public function rules()
{
    return [
        'email'             => 'required|email|unique:employees,email,' . $this->employee,
        'fullname'          => 'required|string|min:4|max:255',
        'password'          => 'nullable|min:6',
        'phone_number'      => 'nullable|numeric|digits_between:0,50',
        'profile_picture'   => 'nullable|image',
    ];
}
php ajax laravel validation
2个回答
2
投票

我想我对这些类型的表单和PUT请求有问题,我猜你也在上传表单中的文件?

我建议尝试使用POST来查看它是否有效(路由也需要更新)。

除此之外,查看ajax请求中发送的数据会很有帮助。


0
投票

您确定您的输入与后端定义的名称相同吗?

© www.soinside.com 2019 - 2024. All rights reserved.