简体   繁体   中英

How to use formdata in JavaScript

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

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

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM