1

I want sending formdata including objects in objects

let submit_obj = { test : { kor : "korea" , eng : "usa"} }
let test_form = new FormData();
test_form.append(test, submit_obj );

    $.ajax({
        url : '/api/filesave',
        type : 'post',
        dataType : 'josn',
        data : test_form,
        contentType : false,
        processData : false,
        success: function(res){
            console.log('res',res);
        }                   
    });

and API response

req.body = Object { test : "[object Object]" }

How to extract the test value?? Thanks for your answer

4
  • 1
    Why are you using Form Data with an Object? Do you want to pass the object up to the server in one piece? jQuery handles it for you without FormData... Commented Oct 26, 2020 at 2:31
  • What data format does the backend code expect? How is it attempting to use the data from the request? Commented Oct 26, 2020 at 2:34
  • 1
    there is a typo - dataType : 'josn', Commented Oct 26, 2020 at 2:36
  • I would like to send this information along with the image file. Commented Oct 27, 2020 at 2:49

2 Answers 2

2

FormData.append() will convert value to string. In that case test : "[object Object]" is absolutly correct value;
So, You can try to use JSON.stringify:

test_form.append(test, JSON.stringify(submit_obj));

or Blob constructor:

test_form.append(test, new Blob([JSON.stringify(submit_obj)], {type:'application/json'}));
Sign up to request clarification or add additional context in comments.

Comments

-1

Please do not use dataType = JSON.

jQuery.ajax({
    url: '/api/filesave',
    data: test_form,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});


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.