Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JQuery中解析集合_Javascript_Jquery_Asp.net - Fatal编程技术网

Javascript 如何在JQuery中解析集合

Javascript 如何在JQuery中解析集合,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我有一个自定义数据类型的IEnumerable集合,正在发送给客户端。 我想在JQuery方法中解析集合。目前我得到的值为“未定义”。下面是我的代码: 服务: [OperationContract] [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)] IEnumerable<CustomData> GetSetting(long user

我有一个自定义数据类型的IEnumerable集合,正在发送给客户端。 我想在JQuery方法中解析集合。目前我得到的值为“未定义”。下面是我的代码:

服务:

 [OperationContract]
 [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
 IEnumerable<CustomData> GetSetting(long userId);

  public IEnumerable<CustomData> GetSetting(long userId)
  {
        var tempData = Context.DialogSettings.Where(item => item.id == userId).ToList();
        return tempData.Select(dialogSetting => new CustomData { KeyName = dialogSetting.KeyName, KeyValue = dialogSetting.KeyValue }).ToList();
  }


[DataContract]
public class CustomData
{
   [DataMember]
   public String KeyName;
   [DataMember]
   public String KeyValue;
 }

从对这个问题的评论中,我可以有把握地推测以下js代码将起作用:

if(typeof data != 'undefined'){
    alert(data[0].KeyName); //this will yield a value.
}
else
    alert('Ok. This is weird');

您是否尝试过
警报(数据)
?除了thg435所说的,如果您发送集合/列表,您不希望
数据
是一个对象数组吗?在ajax成功回调中,
data.length
是否也未定义?如果它是一个列表,为什么
data
会有一个
d
var?我尝试了警报(数据)并获得了类似[object object object object]的4项(4项,因为方法GetSetting()会向客户端返回4项),但我不知道如何解析[object object]在client side.ok,当您在浏览器中键入“your.host/SampleService.svc/GetSetting?userId=1”时,您会得到什么?谢谢。当通过API2控制器返回IEnumerable时,您的解决方案起作用
if(typeof data != 'undefined'){
    alert(data[0].KeyName); //this will yield a value.
}
else
    alert('Ok. This is weird');