5

Sorry this is a duplicate from here asked in SO but I'm new to this so I would like to know how to do it?

This is my ajax call:

  $("#btnprocess").click(function () {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetFilenames",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.d[0]);
                        alert(response.d[1]);
                      }
                });
  });

Individually I'm able to get the response but I need to loop them.

Can anyone say me how do I do this?

2
  • 1
    @xander-that dosent depend on how many points you gain it's a matter of learning something new Commented Apr 20, 2012 at 18:48
  • 1
    @all-Thanks for your response. Commented Apr 20, 2012 at 18:51

3 Answers 3

12

Use $.each().

$.each(response.d, function(key, value) {
    //For example
    console.log(key + value)
})

Look here to learn about it. (EDIT: Or here - it's a video tutorial if you prefer that.)

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

Comments

7

if response.d is an array you could place it in a for loop like so:

for ( var i = 0; i < response.d.length; i++ ) {
    // do action here
}

This method is preferred over the jQuery $.each() function due to its speedier nature. Check out this Fiddle for a comparison of for vs $.each().

Comments

6

You could do;

for (var i=0; i<response.d.length; i++) {
 alert(response.d[i]);
}

Comments

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.