Search code examples
ajaxarraysjsoncountjquery-callback

Ajax Json count elements in callback array


I have a simple Ajax Call and want to count the elements in the data array that comes back. But .length() doesn't work somehow. Do you know a better way? Thank you!

My Code so far:

$.ajax({
                async:   true,
                type:        'post',
                cache:       false,
                url:         'location.php',
                data:        {myJson: dataArray},
                dataType   : 'json',
                success: function(data){
                    var ArrayContent = data.length();
                    if(ArrayContent == 2) {
                        $('#Result').removeClass('loading').empty();
                        $('#PlzInput').val(data[0]);
                        $('#Result').html('Ihr Standort: '+data[1]);
                    } else if( ArrayContent == 1 ) {
                        $('#Result').removeClass('loading').empty();
                        $('#Result').html(data[0]);
                    }
                }
        });

Solution

  • Remove the braces. i.e:

    var ArrayContent = data.length;