JSONP request to your WebAPI service

A day ago, I was trying to prepare a demo on how simple it is to make a cross domain AJAX request to a web service, and display the JSON result in a friendly HTML div using jQuery AJAX method.

I created a simple WebAPI controller, which had a method which returns a IEnumerable<MyObject> of a list of objects, and published it to a Windows Azure Cloud Service. The Web Service was working like a charm.

When I created the jQuery code, which was as simple as this

$(function () {
var url = "http://webprogramming.cloudapp.net/api/Kompanite?callback=?"

$.getJSON(url, function (data) {
$.each(data, function (index) {
//place info in a div
});
});
});

and was supposed to work smoothly. But, as opposed to a working code, I started to receive no results. I started to catch ajax errors, and saw that the callback function was not being called. After three hours of research, I came through this “JSONP with ASP.NET WebAPI” StackOverflow question, which explained everything. The answer was that, by default, the result of WebAPI was not prepared to deal with callback required for JSONP. You need to have a special JsonMediaTypeFormatter which can handle this kind of situation. After I copied/pasted the formatter class from the answer, and cofigured it in Global.asax’s Application_Start() method through this line:

config.Formatters.Insert(0, new JsonpMediaTypeFormatter());

the jQuery code started to work like a charm. So… if you fall into a similar situation, please make sure you implement your own JsonMediaTypeFormatter to support JSONP calls to your web service.

Social Innovation Camp Kosovo

Last weekend, I had the pleasure and opportunity, to be mentoring for the second Social Innovation Camp Kosovo, together with Dan McQuillan, Fisnik Ismaili, Laura HahnPeter Manning, and Josh Harvey .

 

During a 48 hours challenge, six groups of young Kosovars tried to make reality the idea they had proposed, which intended to make a social impact in Kosovo. After 48 hours of hard work and fun, teams came up with the prototypes of their ideas, went out to test their prototypes with other people, and then presented their prototype together with the impressions gathered outside, in front of the independent judge panel.

After all the fun and hard work, three teams excelled and won prices to continue forward their ideas. The winners were:

  • Eventor – A project which intends to put together events and scholarships for Kosovars, and make them available for students and others to get informed.
  • KapLexo – An idea around book exchanging for second hand books, while making also possible for people to review, comment, meet and discuss, about the books they like and have read.
  • ParkingLot – An idea which intends to make free parking slots in maps (web and mobile) through social participation.

Personally, I would like to thank UNICEF Innovation Labs Kosovo for making this event possible, which creates opportunities for young people to start new projects which could have positive impact in Kosovo.