i have the following code: (simplified for readability)
C#
foreach(DataRow dRow in myDS.Tables[0].Rows){
Company myCompany = new Company();
myCompany.id = int.Parse(dRow["id"].ToString());
Companies.Add(myCompany);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
Response.Write(serializer.Serialize(Companies));
Response.End();
jQuery
$.getJSON('ajax.aspx?what=' + $('input[name="what"]').val() + '&where=' + $('input[name="what"]').val(), function (data) {
$.each(data, function (key, val) {
var myCompany = new function () {
this.id = val.id;
}
Companies.push(myCompany);
});
});
Now, i have another object in the C# code, which is named Cities
and i would like to return it in the same request.
something like
Response.Write(serializer.Serialize(Companies));
Response.Write(serializer.Serialize(Cities));
Response.End()
and offcourse parse it on the Client side.
how can i do something like that?
You could wrap the two properties into an anonymous object:
Or if you are using some old .NET version which doesn’t support anonymous objects you could define a wrapper class:
and then:
Also fix your AJAX call as you are not properly url encoding your parameters: