3

我正在尝试将 JavaScript 对象作为字典传递给 C# WCF 服务,但我不知道该怎么做......

我有一个 WCF 服务:

[OperationContract]
[WebInvoke]
public List<psy_trance_fm_genre> select(SortedDictionary<string, object> parameters)
{
    ...
}

我有一些 JavaScript / JQuery 代码:

$.ajax({
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ '@genre': '', '@start_row_index': 0, '@maximum_rows': 100 }),
    error: function (jqXHR, textStatus, errorThrown) {
        ...
    },
    success: function (data, textStatus, jqXHR) {
        ...
    },
    type: 'POST',
    url: 'svc/psy_trance_fm_genres.svc/select'
});

请帮助我让他们一起工作!提前致谢!

4

2 回答 2

1

也许这会帮助你或让你朝着正确的方向前进:

我正在使用的 Ajax 调用示例:

    var data2Send = {
        "CategoryID": CatID, "AccountID": AccID, "RegionID": RegID, "PersonID": PerID, "BudgetID": BudID,
        "AccountDetailTypeID": AdtID
    }
//data2Send could also contain i.e. an array of arrays, list of lists, etc


    $.ajax({
        type: "GET",
        url: '../../Service/myservice.svc/GetAccountDetails',
        dataType: "json",
        data: data2Send,
        contentType: "application/json; charset=utf-8",
        success: function (data) { var accountDetails = JSON.parse(data.d); },
        error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); }
    });

WCF 合同:

    [WebInvoke(Method = "GET")]
    [OperationContract]
    string GetAccountDetails(int AccountID = 0, int RegionID = 0, int PersonID = 0, int CategoryID = 0, int BudgetID = 0, string AccountDetailTypeID = "");

请注意,合约返回一个字符串。我这样做使用:

        var accountDetailList = db.AccountDetails
            .Select(ad => new
            {
                AccountDetailTypeID = ad.AccountDetailTypeID,
                Reference = ad.Reference,
                Description = ad.Description,
        AccountDetailID = ad.AccountDetailID,
            })
            .ToList();

return JsonConvert.SerializeObject(accountDetailList);

我希望这有帮助。

于 2013-07-23T00:10:54.557 回答
0

I decided to rewrite my service to use only strings and int as parameters.

As far as I understand, it's impossible to pass the dictionary in a simple straightforward way.

于 2013-07-23T19:17:19.323 回答