javascript 对象的服务器端类型是什么,使用 JSON 序列化客户端,通过 ajax 调用传递?
这不是最清晰的问题标题,对此感到抱歉,但我会尽力分解我的问题。
我有一些客户端 javascript,并且正在尝试进行 ajax 服务器端调用,传递一些数据。我正在使用 JSON 序列化这些数据。
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: stringifyData(dataToStringify)
dataType: "json",
});
stringifyData = function (dataToStringify){
var requestParameters = {};
requestParameters.requestData = dataToStringify;
return JSON.stringify(requestParameters)
}
这是我正在使用的代码,效果很好。
//client side JS
var dataToStringify = 'John'
//Becomes- '{"requestData":"John Smith"}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(string requestData){
//stuff
}
如果我序列化一个简单的字符串,它会反序列化为一个字符串。
//client side JS
var dataToStringify = {}
dataToStringify.FirstName = 'John'
dataToStringify.SurName = 'Smith'
//Becomes- '{"requestData":{"Firstname":"John","Surname":"Smith"}}'
//server side c#
public class Person {
string Firstname {get;set;}
string Surname {get;set;}
}
[WebInvoke(Method = "POST")]
public string ajaxCall(Person requestData){
//stuff
}
如果我序列化一个 javascript 对象,那么它会反序列化为适当的服务器端对象(已定义所有键)。
//client side JS
var dataToStringify = {}
dataToStringify.Key1 = 'Val1'
dataToStringify.Key2 = 'Val2'
...
dataToStringify.Keyn = 'Valn'
//Becomes- '{"requestData":{"Key1":"Val1","Key2":"Val2"...,,"Keyn":"Valn"}}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(??? requestData){
//stuff
}
我的问题是我试图传递一个对象,其中的键并不全部已知 - 一个简单的键/值对列表。但我无法让它正确反序列化。使用“字典”作为服务器端类型不起作用,通用“对象”也不起作用。
在我的最后一个示例中,reqestData 对象应该是什么类型?我想要实现的目标可能吗?
非常感谢。
This is not the clearest of question titles, sorry about that, but I will try and break down my issue as best I can.
I have some client side javascript, and am trying to make an ajax server side call, passing through some data. I am serialising this data using JSON.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: stringifyData(dataToStringify)
dataType: "json",
});
stringifyData = function (dataToStringify){
var requestParameters = {};
requestParameters.requestData = dataToStringify;
return JSON.stringify(requestParameters)
}
This is the code I am using, which works fine.
//client side JS
var dataToStringify = 'John'
//Becomes- '{"requestData":"John Smith"}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(string requestData){
//stuff
}
If I serialise a simple string, it deserialises to a string.
//client side JS
var dataToStringify = {}
dataToStringify.FirstName = 'John'
dataToStringify.SurName = 'Smith'
//Becomes- '{"requestData":{"Firstname":"John","Surname":"Smith"}}'
//server side c#
public class Person {
string Firstname {get;set;}
string Surname {get;set;}
}
[WebInvoke(Method = "POST")]
public string ajaxCall(Person requestData){
//stuff
}
If I serialise a javascript object, then it deserialises to an appropriate server side object (has all keys defined).
//client side JS
var dataToStringify = {}
dataToStringify.Key1 = 'Val1'
dataToStringify.Key2 = 'Val2'
...
dataToStringify.Keyn = 'Valn'
//Becomes- '{"requestData":{"Key1":"Val1","Key2":"Val2"...,,"Keyn":"Valn"}}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(??? requestData){
//stuff
}
My problem is that I am trying to pass an object where the keys are not all known - a simple list of key/value pairs. But I cannot get it to deserialise correctly. Using 'Dictonary' as the server side type does not work, nor does a generic 'Object'.
In my last example, what type should the reqestData object be? Is what I am trying to achieve possible?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Etch 的答案是正确的,但如果我没记错的话,
KeyValuePair
不太适合序列化。你可以定义一个你自己的简单的 (key, value) 结构,并传递它:
然后你的 json 应该是这些的数组,看起来像:
[ {'Key' : 'key 1', 'Value': 'value 1'}, {'Key' : 'key 2', 'Value': 'value 2'}]
@Etch's answer is on the right track, but if I remember correctly,
KeyValuePair
isn't very serialization-friendly.you can define a simple (key, value) struct of your own, and pass that:
and then your json should be an array of those, looking something like:
[ {'Key' : 'key 1', 'Value': 'value 1'}, {'Key' : 'key 2', 'Value': 'value 2'}]
如果您将调用服务的 json 更改为,
则应与
KeyValuePair
配合使用。我没有尝试过使用KeyValuePair
,但我以这种方式使用List<>
。编辑
查看我的服务后,我注意到我不使用
List<>
,但我使用数组。所以在你的情况下:If you change your json for calling the service to
That should work with
KeyValuePair<string,string>
. I havent tried to useKeyValuePair
but I useList<>
in this fashion.Edit
After looking over my services I noticed I do not use
List<>
however I use an array. so in your case: