0

I'm working with Laravel API and calling an API method in two different way. In one-way form validation working but in second-way form validation not working. But I really need second-way of API call.

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);
        });
}

Doing form validation in Laravel Request Controller.

3
  • What's inside member_info? Commented Jan 19, 2019 at 8:48
  • Can you let us know why you need to use FormData? Commented Jan 19, 2019 at 11:17
  • FormData is using to upload photo at the same time. Commented Jan 20, 2019 at 9:27

1 Answer 1

0

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)
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.