Reputation: 1381
I have a form that is used to upload multiple files on click of submit button.
For example i am uploading the following files
student.png
animal.png
pet.png
I am using the following ajax to send the values at the backend (code reference taken from).
$.ajax({
type:"POST",
url:this.vpb_settings.vpb_server_url,
data:dataString,
cache: false,
contentType: false,
processData: false,
success:function(response)
{
console.log(response);
}
});
When i try to print the response through "console.log()", i get the list of the names of the files that were uploaded, which is one of my requirement. So after using "console.log(response)" i get the output as
student
animal
pet
Now i wish to to calculate the number of names that i get, for this i used this code, "console.log(response.length)" but i got the output as
7
6
3
Whereas i want the response as 3 because there are 3 items in the list
can anyone please tell how this can be done
Upvotes: 0
Views: 42
Reputation: 3257
Looks like you are using one ajax call per file, so of course, each response
is the name of one file (a string), not an array, hence
'student'.length
=> 7'animal'.length
=> 6'pet'.length
=> 3If you need the amount of files you could use a global counter, like this:
var counter = 0; //<--needs to be declared somewhere it gets initialized once only
function someFuncContainingTheAjaxCall(){
$.ajax({
type:"POST",
url:this.vpb_settings.vpb_server_url,
data:dataString,
cache: false,
contentType: false,
processData: false,
success:function(response)
{
console.log('Files so far ' + (++counter));
}
});
}
HIH
Upvotes: 1
Reputation: 630
You can create any variable in outer scope and increase it on success callback. Also you will need to reset it to 0 before do call.
Upvotes: 0