将JavaScript对象作为Dictionary<字符串,对象>
Passing JavaScript object to C# WCF service as Dictionary<string, object>
我正试图将JavaScript对象作为Dictionary传递给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作为参数。
据我所知,用简单明了的方法传递字典是不可能的。
相关文章:
- 如何使用jquery读取对象字符串
- 用javascript修复这个JSON对象字符串最干净的方法是什么
- Javascript对象字符串值-在应该隐藏的时候呈现的HTML代码
- 在Nodejs中将数组转换为类似子的对象字符串
- 对象字符串转换为JSON(抛出错误)
- 如何将事件对象字符串化
- 如何在 JavaScript 中将 JSON 对象字符串化为负零
- 将 JSON 对象字符串数组转换为 JS 对象数组
- 如何在角度转换请求中将对象字符串转换为 JSON 对象
- 删除开头的双引号&结束于JSON对象/字符串或Java脚本变量
- 将json对象字符串转换为值
- 如何迭代json对象字符串
- 将数组列表转换为json对象字符串
- 如何将继承的对象字符串化为JSON
- 如何按对象字符串值之一对对象数组进行数字排序
- 解析日期对象字符串并获取以毫秒为单位的时间
- 如何在javascript中解析javascript对象字符串
- Javascript类对象字符串到对象
- 从使用.data()添加的对象字符串中获取值
- 如何在javascript中添加一个函数变量到对象字符串