I seems like I did know this, but I got reminded of it Thursday night and it surprised me a little. I made this example today:
using System.Collections.Generic;
using System.Web.Http;
namespace WebApiExperiment.Controllers
{
public class ExperimentController : ApiController
{
public Dictionary<string, int> Post()
{
var dictionary = new Dictionary<string, int>();
dictionary.Add("foo", 1);
dictionary.Add("bar", 2);
dictionary.Add("baz", 3);
dictionary.Add("qux", 4);
return dictionary;
}
}
}
I latched ahold of the "dictionary" in JSON form from a view as seen below. I tried at first to make a dictionary of int,string instead of the other way around, but there was then no way to call into the serialized blob that returned in a shape like so: result.1 (result.foo is golden however)
<script src="/Scripts/jquery-1.8.2.js"></script>
<script language="javascript">
$(function () {
myAsynchronousThing();
});
function myAsynchronousThing() {
$.ajax({
type: "POST",
url: '/api/Experiment/',
dataType: 'json',
success: function (result) {
alert(result.qux);
$('#result').html(JSON.stringify(result));
}
});
}
</script>
<div id="result" style="z-index: 10;"></div>
I bring this all up because I saw Blake McCabe speak at Polyglot Programmers on Thursday night. He presented on an app he made that returned serialized IEnumerable collections. He was calling the ASP.NET Web API from jChartFX charts and they were cool too.
No comments:
Post a Comment