【问题标题】:WCF jQuery receiving list data from serverWCF jQuery 从服务器接收列表数据
【发布时间】:2013-03-26 07:31:01
【问题描述】:

我正在尝试使用 jQuery + Ajax + WCF...你能帮我吗?如果我在 WCF 中使用单个数据响应是可以的,但是当我尝试从 WCF 发送一个列表或大量数据作为响应时,它看起来是错误的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace longpollingexample
{
    [ServiceContract(SessionMode = SessionMode.NotAllowed)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service2
    {          
        static List<int> ff = new List<int>();

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        public List<int> hi(int a, int f)
        {
            ff.Add(a*f);
            return ff;
        }
    }
}

这是 WCF 服务,我们只有一个功能并尝试获取列表的答案。列表是静态的。如何从该列表中获取任何元素?我想做什么......

  function hi2() {
        var t = 0;
        $.ajax({
            async: true,
            url: '/Service2.svc/hi',
            data: 'a= ' + $('#my1').val() + '&f=' + $('#my2').val(),
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var str='';
                str += data[0].d;
                $("#msgs").html(aa);
            },
            error: function () { alert('pop the champagne'); }
        });
    }  

我可以得到data[0].d - 我的ff 列表中的第一个元素吗?它不起作用(怎么了?谢谢!对不起我的英语,我知道它真的很糟糕(

【问题讨论】:

  • 这对你有用吗??

标签: jquery asp.net ajax wcf


【解决方案1】:

就这样吧,

 success: function (data) {
  $.each(data.d, function(i, item) {
         alert(item);
   });
 }

【讨论】:

  • 可以,但是我可以处理海量数据[0]、数据[1]等数据吗?
  • @KarenLi - 我不确定,但你可以像 data.d[0], data.d[1] ...etc bcasue in data.d 你有你需要的 json 对象像我在我的代码中所做的那样旋转...直接数据 [0] 是不可能的..您可以在 jquery 网站上查看此文档..
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 2015-06-09
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多