As I am paying with my JavaScript jQuery MVC framework, I have been thinking about what would be the mid tier and JSONp because of it's speed and cross domain abilities seems to be like a great solution. So I was looking around the internet, and there are not that many JSONp feeds available. The world still thinks in of XML in terms of syndication. So I put together a little demo that shows you how you can create a service that will convert any RSS (or XML) feed into JSONp.
This blog has a code sample how to add a property on ASP.NET MVC ActionResult that will convert to JSON result into JSONp. After you add this code into your project, you can use the following action result:
[JsonpFilter]
public ActionResult rss2jsonp(string id)
{
// Request URL of the RSS feed.
string webUrl = Request.QueryString["url"];
// Load RSS as string.
WebClient client = new WebClient();
string result = client.DownloadString(webUrl);
// Convert string to XDocument.
XDocument xmlDoc = XDocument.Parse(result);
// Run a LINQ statement againts XDocument.
var q = from c in xmlDoc.Descendants("item")
select new
{
title = (string)c.Element("title"),
link = (string)c.Element("link"),
pubDate = (string)c.Element("pubDate"),
description = (string)c.Element("description")
};
// Return JSON (actually JSONP).
return Json(q, JsonRequestBehavior.AllowGet);
}
Then, you can write JavaScript like that will call your service:
(function ($) {
var siteUrl = "http://feeds.nytimes.com/nyt/rss/HomePage";
// JSONP call.
$.ajax({
dataType: "jsonp",
url: "rss2jsonp/testUrl?url=" + siteUrl,
jsonp: "callback",
success: function (d) {
console.log(d);
}
});
} (jQuery))
As I mentioned there are some great benefits in using JSONp: first it works cross-domain. This mean that not only you can do full distributed services in your architecture, but it also lets you fully separate UI development from your "server" code which usually needs complicated setup, and compilation. I mean you can with JavaScript MVC and JSONp as your data service, you can literally develop inside of your desktop folder.