Laravel API调用表单验证不起作用

时间:2019-01-19 08:31:59

标签: laravel rest 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 Request Controller中进行表单验证。

1 个答案:

答案 0 :(得分:0)

看起来Json Object可以发送到API。 请求致电修复时,我错过了您的错误。

但是我认为您需要将FormData转换为Json Object:

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

然后重新发送:

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