从WCF服务返回数据表时在fiddler中获取错误

时间:2013-07-22 13:58:42

标签: knockout.js datatable wcf-rest

我正在尝试从wcf服务返回数据表。我写了服务。其代码如下

 [WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "TableData")]
    public DataTable GetData(Parameters param)
    {
        try
        {


            var Id = int.Parse(param.Id);
            var tableName = param.TableName;
            var listSelectCol = new ArrayList();
            bool bFirst = true;
            var sSelectColList = new StringBuilder();
            var Query ="";
            var List = from TableEntity in _TableEntityRepository.AsQueryable()
                                  join ColumnEntity in _ColumnEntityRepository.AsQueryable() on TableEntity.Id equals ColumnEntity.Id
                                  where sysTableEntity.TableName.Equals(tableName)
                                  select ColumnEntity.Name;



            foreach (var x in List 
            {
                if (bFirst)
                    bFirst = false;
                else
                    sSelectColList.Append(", ");

                sSelectColList.Append("t." + x.ToString(CultureInfo.InvariantCulture));
                listSelectCol.Add(x.ToString(CultureInfo.InvariantCulture));


            }

                 Query =
                    "select " + sSelectColList + " ,  t.Name as MyName  from  " + tableName +
                    " t   Where t.Id = " + Id;





            var cmdQuery = new SqlCommand(Query, new SqlConnection(ConfigurationManager.AppSettings.Get("connectionString")));
            var sdaQuery = new SqlDataAdapter(cmdQuery);
            var diffTableFrom = new DiffTable();
            var colValues = new DataColumn("ColValues", typeof(System.String));

            var dt = new DataTable();
            dt.Columns.Add(colValues);

            sdaQuery.Fill(dt);

            diffTableFrom.ReturnDiffTable = dt;

            foreach (DataRow row1 in dt.Rows) 
            {
                foreach (string i in listSelectCol)
                {
                    row1["ColValues"] = row1["ColValues"] + row1[i].ToString() + ",";

                }
                row1["ColValues"] = row1["ColValues"].ToString().Remove(row1["ColValues"].ToString().Length - 1);


            }


            return dt;

        }

        catch (Exception e)
        {



            throw new WebFaultException<string>(
        string.Format(
            "{0}{1}",
            e.Message, (e.InnerException != null ? e.InnerException.Message : string.Empty)), HttpStatusCode.BadRequest);
        }


    }

DiffTable的类是

  public class DiffTable
    {
        public DataTable ReturnDiffTable {get;set;}
    }

来自我的viewmodel的调用代码是

function loadData(id, tablename) {
        debugger;

        var input =
       {

           TableName: tableName,
           Id: id

       };


        return $.ajax({
            url: "../Service/TableData",
            type: "PUT",
            contentType: 'application/json',
            processData: false,
            data: JSON.stringify(input),
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert(errorThrown);
            },
           success: function (allData) {
                var mappedData = $.map(allData, function (item) {
                    return new TableChangesDetail(item);

                });

                self.TableChangesDetailList(mappedData);


            }

        });

    }

当我尝试使用ajax从我的viewmodel调用此服务时,我在fiddler中收到错误[Fiddler] ReadResponse() failed: The server did not return a response for this request.

如何从wcf服务返回数据表?当我调试服务并在返回dt时放置断点;然后我得到1排。因此,数据从数据库返回,但不返回到viewmodel。

我在这里做错了什么?

UPDATE1

我改变了在我的服务中写的这一行

var dt = new DataTable(); 

var dt = new DataTable("Test");

我现在没有收到任何错误。但是fiddler没有收到json格式的数据。当我打开fiddler并可以在TextView选项卡下以某种格式查看数据。这就是我没有进入淘汰模型的原因。我现在如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我设法找到另一种方式。我不确定这是否聪明。我所做的是将数据表转换为列表和返回列表。所以我在我的服务中添加了以下代码

foreach (DataRow row1 in dt.Rows) 
            {
                var list = new LookUpTable();
                list.Name = (string) row1["Name"];
                list.Id = row1["Id"];
                myReturnList.Add(list);


            }


return myReturnList.ToList();