[英]Passing JavaScript object to C# WCF service as Dictionary<string, object>
我正在嘗試將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'
});
請幫助我讓他們一起工作! 提前致謝!
也許這會幫助你或讓你朝着正確的方向前進:
我正在使用的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);
我希望這有幫助。
我決定重寫我的服務,只使用字符串和int作為參數。
據我所知,以簡單直接的方式傳遞字典是不可能的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.