3

I have an Ajax call that returns an array and need to do something with each of the values from this array.

So far I have the below but this returns the following error:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in array(5)...

Can someone tell me what I am doing wrong here resp. how I can do something with each of the values ?

My Ajax:

$.ajax({        
    type: "post",   
    url: "ajax.php",
    cache: "false",
    data: {
        node: 'fetchValues',
        itemIDs: itemIDs
    },
    success: function(data){
        console.log(data);  // for testing only
        jQuery.each(data, function(index, value){
            console.log(value);
        });
    }
});

Example for "data" (from console log):

array(5) {
  [1]=>
  string(6) "Value1"
  [2]=>
  string(6) "Value2"
  [3]=>
  string(6) "Value3"
  [4]=>
  string(6) "Value4"
  [5]=>
  string(6) "Value5"
}

Many thanks in advance for any help.

8
  • Have you tried a simple for? var x=0; for(x;x<data.length;x++) { var value=data[x]; } Commented Jul 20, 2015 at 11:10
  • try this one for(var x in data){ console.log(x + ' :' + data[x]); Commented Jul 20, 2015 at 11:13
  • @RicardoPontual: Yes, I tried this before but it returns an error referring to the length. Commented Jul 20, 2015 at 11:32
  • @HadiHassan: Thanks for this. Wouldn't I need to use this with a loop ? Commented Jul 20, 2015 at 11:32
  • @TaneMahuta I didn't get what you mean, but the comment i provided above will loop to each property in the returned object via ajax Commented Jul 20, 2015 at 11:34

2 Answers 2

5

seems like your array not parsed properly

from php side before sending response

echo json_encode($result); // REPLACE $result WITH  YOUR OUTPUT ARRAY

On jquery side:

$.ajax({        
    type: "post",   
    url: "ajax.php",
    dataType : 'JSON',
    cache: "false",
    data: {
        node: 'fetchValues',
        itemIDs: itemIDs
    },
    success: function(data){
        console.log(data);  // for testing only
       var data=$.parseJSON(data);
        jQuery.each(data, function(index, value){
            console.log(value);
        });
    }
});

Ref: http://api.jquery.com/jquery.parsejson/

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for this. I had tried something similar before but this returns the following error: "Uncaught SyntaxError: Unexpected token a... "
@TaneMahuta before sending response are use using json_encode() ? Whats your server side script? Is it PHP?
Not here. Should I try that ?
Whats your server side script? Is it PHP?
Yes - currently I am just dumping the array there.
|
1

You need to parse the string in your data variable to an object:

success: function(data){
    console.log(data);  // for testing only
    var res=$.parseJSON(data);
    jQuery.each(res, function(index, value){
        console.log(value);
    });
}

You can either use

var res=$.parseJSON(data)

Or plain JS:

var res=JSON.parse(data)

Source

5 Comments

Thanks for this. I had tried something similar before but this returns the following error: "Uncaught SyntaxError: Unexpected token a... " - same as for the answer above.
Try giving $(data) in $.each instead of plaint data
Thanks - you mean to change this in your parseJSON line and to use $.each instead of jQuery.each ?
Thanks - there is no data in your jQuery.each. :)
ok. just console.log(res) and console.log($(res)); using my method and let me know what you are getting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.