Adem Natural Adem Natural - 4 months ago 10
jQuery Question

how to put double variable data in ajax

i've case to use variable more the one like code bellow :



function upload(iddata,ring)
{
//variable 1
dataString ="iddata="+iddata+"&ring="+ring+""; // variable 1

//variable 2
var formData = new FormData();
for (var i = 0; i < filesToUpload.length; i++) {
var file = filesToUpload[i];
formData.append("file[]", file, file.name);
}

// i want put both variabel in ajax like this
$.ajax({
data: {formData,dataString}, //variable 1 and 2 in group
});
}





please how source is True

Answer

You should handle your whole data as an object only, and not mix it as a string and a array in a object. In this case you would not get the expected data in your receiver.

You could change it like this:

function upload(iddata, ring) {
    var data = {
        iddata: iddata,
        ring: ring,
        formData: new FormData()
    };

    for (var i = 0; i < filesToUpload.length; i++) {
        var file = filesToUpload[i];
        data.formData.append("file[]", file, file.name);
    }

    $.ajax({
        data: data
    });
}
Comments