I’m calling an asp.net webservice with JQuery and ajax, transfering data with json.
I’m creating javascript objects that will be json stringified. I need my webmethod to retrieve these particular object types but my parameter type is a base class and those objects inherit from my base class like this :
[DataContract]
[KnownType(typeof(TextareaObject))]
[KnownType(typeof(TextObject))]
public class FormElement
{
public FormElement()
{}
}
and :
[DataContract(Name = "textObject")]
public class TextObject : FormElement
{
[DataMember]
public string question { get; set; }
public TextObject(string question)
{
this.question = question;
}
}
and my webmethod :
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
[ServiceKnownType(typeof(TextObject))]
[ServiceKnownType(typeof(TextareaObject))]
public void SaveForm(List<FormElement> formobjects)
{
...
}
And here’s the way I’m creating javascript objects (I copy only relevant samples of my code) :
//objects to serialize
function textObject(question) {
this.question = question;
}
//objects to serialize
function textareaObject(question, rownumber) {
this.question = question;
this.rownumber = rownumber;
}
var objectarray = new Array();
if (type == 'text') {
textobject1 = new textObject(typedquestion);
objectarray.push(textobject1);
}
else if (type == 'textarea') {
var rownumber = $(elm).children('textarea').attr('rows');
textareaobject1 = new textareaObject(typedquestion, rownumber);
objectarray.push(textareaobject1);
}
var formobjects = JSON.stringify(objectarray);
$.ajax({
type: "POST",
//Page Name (in which the method should be called) and method name
url: urlhtml,
data: '{"formobjects":' + formobjects + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//dosmth
}
});
And I want asp.net server to be able to deserialize the correct type in my array.
But once in my webmethod, “formobjects” are all of type FormElement, I can’t get their real type even with serviceknowntype attributes. Is is because javascript isn’t strongly typed that I can’t retrieve the concrete types ? because the stringified json won’t give the concrete type?
I tried with
textObject.prototype = new textObject(typedquestion);
objectarray.push(textObject.prototype);
and the json gives something like :
{"formobjects":{"textObject": {"question":"test"}}}
But server side same old same old, I only get FormElement type in my webmethod and I can’t cast.
maybe what I wanna do is not possible..
thank you anyway !!
well I found my answer. Like rich.okelly said, there is no type information sent with weakly-typed language that is javascript, because javascript classes aren’t real classes like in .net. But Microsoft anticipated that, you need to use what they call “type hints”, it means you add “__type” property to your javascript objects, and in the first position like so :
or even add it to the class itself so you don’t add to set it each time:
adding __type after question won’t work. Plus, even if there is no namespace you need to add “:#” after it. the schema follows : “datacontractname:#datacontractnamespace”.
I found it programatically creating serializer on my TextObject so that I can find the needed format. Then my formelement received in my webmethod is type of TextObject ! job done !
thank you all !