Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a list of strings returned from a web service using JQuery

This is my first JQuery experience and I'm on quite a tight deadline. It's a bit of an embarrassing question, but here goes. I'm calling a web service that returns a list of strings (which works and returns OK). Code is below

$(document).ready(
    function() 
    {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "CatList.asmx/GetCatergoies",
            data: "{}",
            dataType: "json",
            success: onActionCompleted
        });
    }
)

function onActionCompleted(data) {

    var resultData = data['d'];
    alert(resultData);
 }

The alert produces a comma seperate string of the results from the web service. I can't figure out for the life of me how I can iterate through the results. When I do something like this:

resultData.each(
   alert(this)
)

All I get in Firebug is this error:

resultData.each is not a function

Any pointers ?

like image 200
Jon Jones Avatar asked Dec 02 '22 07:12

Jon Jones


2 Answers

Using Array.split() will produce an array:

var string = "red,blue,green,orange"

$.each(string.split(','), function(){
  alert(this)
})
like image 197
duckyflip Avatar answered Jan 25 '23 23:01

duckyflip


Consider string.split() instead of jQuery:

var items = results.split(',');

for( var i = 0; i < items.length; i++ ) {
  alert( items[i] );
}
like image 40
Program.X Avatar answered Jan 25 '23 23:01

Program.X