Send array of objects as Ajax parameter

0

I have an array of composite objects as follows:

{file: File(871699), x: 0, y: 0, width: 2, height: 2, …}

Each vector house has an element as described above, and the first parameter, the file is an image that has the following data:

file:File(871699) {name: "tumblr_lxjy8kfFeQ1qh59n0o1_500.gif", lastModified: 1518121346035, lastModifiedDate: Thu Feb 08 2018 18:22:26 GMT-0200 (-02), webkitRelativePath: "", size: 871699, …}

I'd like to pass this array through an ajax call, but I can not. My code is this:

function uploadFiles(formData) {
    
    $.ajax({
        url: '/admin/foto',
        type: 'POST',
        data: arrayObj,
        contentType: false,
        success: () => {
            console.log("Sucesso!!");
        }
    });
}

And on the server I get as follows:

application.post('/admin/foto', (req, res) => {	
  application.controllers.admin.albums.uploadFotos(application, req, res);
});
    
asked by anonymous 21.02.2018 / 22:27

1 answer

0

Give%% of the data.

function uploadFiles(formData) {
    const email = "blablablabla";

    $.ajax({
        url: '/admin/foto',
        type: 'POST',
        data: JSON.stringify(arrayObj), // <<-- Stringify
        contentType: false,
        success: () => {
            console.log("Sucesso!!");
        }
    });
}

I assume you are using JSON.stringify , so your data will be in bodyParser :

application.post('/admin/foto', (req, res) => { 
  console.log(req.body); // <<-- array de objetos
  application.controllers.admin.albums.uploadFotos(application, req, res);
});
    
21.02.2018 / 23:11