首页 > 解决方案 > Laravel API 调用表单验证不起作用

问题描述

我正在使用 Laravel API 并以两种不同的方式调用 API 方法。在单向表单验证中工作,但在第二种表单验证中不起作用。但我真的需要 API 调用的第二种方式。

saveMember: function () {
    let that = this;
    let formData = new FormData();
    formData.append('member_info', that.member_info);

    // member_info is a Json Object
    // This is First way , form validation working good while calling Api
    axios.post('/api/member/save_member', that.member_info)

    // This is second way, form validation not working
    axios.post('/api/member/save_member', that.formData)
        .then(function (response) {
            console.log("response Here: " + response.data.success);
            that.errors = response.data.success;
            // location.reload(true);
        })
        .catch(function (error) {
            that.errors = error.response.data.errors;
            console.log("Error Here: " + error.response.data);
        });
}

在 Laravel 请求控制器中进行表单验证。

标签: laravelrestapi

解决方案


Look like Json Object can send to API. I missing your error when request call to fixed.

But i think you need convert FormData to Json Object:

var obj = {};
that.formData.forEach(function(val, idx){
    obj[idx] = val;
});
var json = JSON.stringify(obj);

and then resend:

axios.post('/api/member/save_member', json)
...

推荐阅读