0

I have a web method in ASP.net which it's output is an ArrayList and read cities from databse. this web method is called using jquery.

$.ajax(
    { url: "../AjaxServices/StateCity.asmx/showcity",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        data: '{s: ' + $('#<%=DpState.ClientID%>').val() + '}',
        success: function(data) {

}

I wanna know how to loop through data, because the data is object. I knew about

jQuery.each( collection, callback(indexInArray, valueOfElement) )

but doesn't work

1
  • Look at the response and the data parameter in Firebug. Commented Mar 24, 2011 at 13:30

2 Answers 2

2

When you call an ASP.Net WebMethod using AJAX, it returns an object with a d property containing your data.

If your WebMethod returns a collection, you can write

$.each(data.d, function(index, obj) { ... });
Sign up to request clarification or add additional context in comments.

Comments

0

Slaks answer is great a ndI found a solution base on SLaks answer, so I'll share it here: just take care about Value and Text they're case sensitive.

success: function(data) { 
                $.each(data.d, function() {   
                  alert(this['Value'] + ':' + this['Text']);                 })             } 

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.