Jquery Form sending data the wrong way

57 views Asked by At

I'm using vue resource module to send POST requests, and jquery form plugin to send to the same routes, but with files.

This is my code

function sendWithoutFiles(data){
    return this.$http.post(link, data)
}
function sendWithFiles(data){
    return new Promise(function(resolve, reject){
        jQuery('#form').ajaxSubmit({
            data: data,
            success: resolve,
            url: link
        })
    })
}

function send(data){
    var sendFunction = sendWithoutFiles;
    if(files)
        sendFunction = sendWithFiles;
    sendFunction({
        message: 'john',
        participants: [1, 50]
    }).then(function(res){
        console.log(res);
    })
}

Now, if I send them without files, it's perfect. But when I do, the data arrives this way:

{
    message: ['john'],
    'participants[]': [1, 50]
}

I'm using express with multiparty like this:

app.post('*', function(req, res, next){
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files){
        req.uploadFiles = files;
        console.log(fields, req.body, "MID LOG");
        req.body = req.body || fields;
        next();
    })
})

In chrome network panel -> request payload enter image description here So I don't think it's something wrong with the multiparty module.

Am I doing something wrong with the jquery plugin ?

0

There are 0 answers