【问题标题】:ajax pass array to wcf serviceajax 将数组传递给 wcf 服务
【发布时间】:2015-01-27 15:24:57
【问题描述】:

遇到问题,我从表中获取数据并将其传递给 wcf 网络服务

第一次服务

public string SaveDate(string data) {
    JavaScriptSerializer json = new JavaScriptSerializer();
    List < string[] > mystring = json.Deserialize < List < string[] >> (data);
    return "saved";
}

第二次获取数据和ajax调用

    $('#btnPaymentOk').click(function () {
        var objList = new Array();
        $("table#tblGridProductInformation > tbody > tr").each(function () {
            if ($(this).find('td.Qty').text() != '') {
                objList.push(new Array($(this).find('td.LocalSalesPrice').text(), $(this).find('td.Total').text()));
                //objList.push(new Array("a", "b"));
            }
        });

        $.ajax({
            type: "POST",
            url: "/WebPOSService.svc/SaveDate",
            data: "{data:" + JSON.stringify(objList) + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            }
        });

    });

看起来像

{data:[["1.00","1.00"],["0.60","0.60"],["0.40","0.40"]]}

json 数组

问题是没有从服务端获取数据

帮帮我

【问题讨论】:

  • 在浏览器控制台网络选项卡中检查请求。需要更多信息

标签: c# jquery wcf


【解决方案1】:

创建数据合约:

[DataContract]
public class Data
{
    [DataMember]
    public List<string[]> data { get; set; }
}

这是我用来测试它的代码,它有效。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace ConsoleApplication
{
    [DataContract]
    public class Data
    {
        [DataMember]
        public List<string[]> data { get; set; }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            string jsonTxt = "{data:[[\"1.00\",\"1.00\"],[\"0.60\",\"0.60\"],[\"0.40\",\"0.40\"]]}";
            var deserialized = JsonConvert.DeserializeObject<Data>(jsonTxt);

            //Use this if you don't want to use DataContract, but you will have to reference Newtonsoft.Json.Linq
            List<string[]> fullDeserialized = JsonConvert.DeserializeObject<List<string[]>>(JObject.Parse(jsonTxt)["data"].ToString());
        }
    }
}

另外,我建议您使用 Newtonsoft 库来序列化/反序列化 JSON 数据。如果您还没有添加对 System.Runtime.Serializiation 的框架引用,请不要忘记添加。

【讨论】:

  • jquery 有问题.. 我不想在 c# 代码中使用它。
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多