如何使用JQuery遍历从Web服务返回的字符串列表 [英] How to iterate through a list of strings returned from a web service using JQuery

查看:90
本文介绍了如何使用JQuery遍历从Web服务返回的字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个JQuery经历,而且我的期限很紧.这是一个令人尴尬的问题,但这是可行的.我正在调用一个返回字符串列表的Web服务(可以正常工作并返回OK).代码在下面

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);
 }

警报会产生一个逗号分隔的字符串,表示来自Web服务的结果.我无法弄清我的一生如何遍历结果.当我做这样的事情时:

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)
)

我在Firebug中遇到的只是这个错误:

All I get in Firebug is this error:

resultData.each is not a function

任何指针吗?

推荐答案

谢谢!没有反馈,我将无法解决.我仍然不完全确定resultData是什么格式.我在代码上做了一些复制和粘贴,所以不确定什么数据['d']将列表转换成json术语.

Thanks! I wouldn't have been able to solve it without the feedback. I'm still not completely sure what format resultData is. I did some copy and paste on the code so not sure what data['d'] converts the list into in json terms.

当我尝试对resultdata进行拆分时,我在Firebug中收到此错误:

When I tried the split on resultdata I got this error in Firebug:

resultData.split不是函数

resultData.split is not a function

最后,我只对结果数据使用了$ .each(),但没有拆分,它可以正常工作.

In the end I just used the $.each() on resultdata without the split and it worked.

function onActionCompleted(data) {

    var resultData = data['d'];
    $.each(resultData, function() {
        alert(this)
    })
}

这篇关于如何使用JQuery遍历从Web服务返回的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆