weixin_33675507 2012-10-23 19:17 采纳率: 0%
浏览 35

通过JSOn结果进行迭代

I have a WebMethod being called from ajax, trying to iterate through the returned data. The data being returned is "{ BusinessTypeID = 2 }". I'm trying to figure out how to just get the value 2?

        //Function called from ajax
[System.Web.Services.WebMethod]
public static string[] GetTerminalBusinessTypes(string terminalID)
{
    DataClassesDataContext db = new DataClassesDataContext();
    List<string> results = new List<string>();

    try
    {

        var terminalBusinessTypes = (from bt in db.BusinessTypes
                                    join obt in db.OxygenateBlenderBusinessTypes on bt.BusinessTypeID equals obt.BusinessTypeID
                                    where obt.OxygenateBlenderID == Convert.ToInt32(terminalID)
                                    select new
                                    {
                                        bt.BusinessTypeID
                                    }).ToList();


        for (int i = 0; i < terminalBusinessTypes.Count(); i++)
        {
            results.Add(terminalBusinessTypes[i].ToString());
        }

    }
    catch (Exception ex)
    {

    }

    return results.ToArray();
}

The ajax function:

            function PopulateTerminalBusinessTypes(terminalID) {

        $.ajax({
            type: "POST",
            url: "OxygenateBlenderCertificationApplication.aspx/GetTerminalBusinessTypes",
            data: "{'terminalID':" + terminalID + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {

                var targetValue = data.d;

                var items = $('#cblTerminalBusinessType input:checkbox');



                                    $.each(targetValue, function (key, targetValue) {
                                        alert(data[index].BusinessTypeID);
                                    });
                    }
        })//end ajax

    }
  • 写回答

3条回答 默认 最新

  • 游.程 2012-10-23 19:32
    关注

    To be honest I cannot see where your "index" is defined.

    Shouldn't the alert line read

    $each(targetVale, function(key, item) {
       // on second look, this wont work as you are converting toString()
       alert(targetValue[key].BusinessTypeId)
       // this should
       alert(item)
    });
    

    You could also throw a debugger; line above the alert and see the values being traversed.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog