| By Joshua Allen | Article Rating: |
|
| December 4, 2008 11:15 AM EST | Reads: |
4,146 |
Joshua Allen's Blog
One of Silverlight’s advantages over Flash is the relatively effortless interop with AJAX. The other day, I needed to mash up some JSON data from various sites, and found it pretty easy to use AJAX to circumvent the crossdomain.xml restriction.
Both Flash and Silverlight allow you to “mash up” data from other web sites, but only if that site has a crossdomain.xml policy file defined. This sucks if you are calling a service like FriendFeed, who can’t make up their mind.
If you’re doing pure AJAX, you can get around these cross-domain restrictions by using JSON. One of Silverlight’s advantages over Flash is the relatively effortless interop with AJAX. The other day, I needed to mash up some JSON data from various sites, and found it pretty easy to use AJAX to circumvent the crossdomain.xml restriction. In the next month or two, my team will release a simple library to make this generic, but in the meantime here is an explanation for anyone who is blocked:
Step 1: Call into JavaScript from Silverlight, passing the URL of the JSON API:
HtmlPage.Window.Invoke("injectScript", url);
Step 2: The JavaScript Function “injectScript” looks like this:
function injectScript(url) {
var head = document.getElementsByTagName(‘head’)[0];
var script = document.createElement(’script’);
script.type = ‘text/javascript’;
script.src = url;
head.appendChild(script);
};
Step 3: Have the JSON script call back to a function in your page called “callback”:
function callback(obj) {
var silverlight = document.getElementById("silverlight");
if (silverlight) {
silverlight.Content.Page.PassData(JSON.stringify(obj));
}
};
Step 4: The callback() JavaScript function passes the data into Silverlight, where it is loaded into a JsonObject:
[ScriptableMember]
public void PassData(string data)
{
JsonObject data = …
}
IMO, this code is cleaner and faster than the standard technique of creating a “WebRequest” from Silverlight. And of course, a WebRequest will fail if the crossdomain.xml is missing.
So, is this a security hole? No! All web browsers on the planet allow cross-domain access to JSON, and if JsonObject.Parse had a “url” parameter, we presumably wouldn’t need to check for crossdomain.xml. The current restrictions in Silverlight undoubtedly result from the fact that WebRequest doesn’t know whether its result is intended for Json, XML (which all web browsers restrict by default), or something else.
Published December 4, 2008 Reads 4,146
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Joshua Allen
Joshua Allen, an Evangelist at Microsoft, is also author of the "Better Life Through Software" blog.
![]() |
Edgar 12/05/08 04:27:12 PM EST | |||
Interesting article. I would like to point out that calling across browser domains will not work in all browsers. Firefox will outright prevent this behavior, and IE will alert the user with a warning. The best way to call into a service that you can't control is to make a call into your server, and then inside you server code forward that call onto the service onto the seperate domain. So the call flow would like something like this: |
||||
![]() |
shadedecho 12/05/08 10:09:39 AM EST | |||
Uhh, how exactly is this really at all different from flash and externalinterface? For instance, flash can easily make calls like: ExternalInterface.registerCallback("flcallback",handleResponse); and the flash actionscript response handler: function handleResponse(str:String):void { and of course the javascript for "injectScript" is the same. and the JS callback function: function callback(obj) { ---------------------------- And btw... this is really (IMHO) not a good idea to circumvent appropriate server-side authorization mechanisms. The "crossdomain.xml" is really a powerful tool, and if a web2.0 service provider can't figure out how to publish a sensible one for mashups to use, then it's THEIR fault, not the fault of the security model. |
||||
- The Top 150 Players in Cloud Computing
- Kindle 2 vs Nook
- 4th International Cloud Computing Conference & Expo Starts Today
- Yahoo! to Keynote 4th Cloud Expo: Accelerating Innovation with Cloud Computing
- Is the PR Business Extinct? Yes
- Exclusive Q&A with Rich Marcello - Unisys President, Systems & Technology
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Ulitzer News: Search vs New Media
- Typhoon Ondoy (Ketsana) and Floods Hit the Philippines
- Publishing Synergy: Blog, Twitter and Ulitzer
- Will PR Firms Survive The New Media Avalanche?
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- The Top 150 Players in Cloud Computing
- Kindle 2 vs Nook
- 4th International Cloud Computing Conference & Expo Starts Today
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Yahoo! Named “Platinum Sponsor” of Cloud Computing Expo
- Yahoo! to Keynote 4th Cloud Expo: Accelerating Innovation with Cloud Computing
- Is the PR Business Extinct? Yes
- Exclusive Q&A with Rich Marcello - Unisys President, Systems & Technology
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Deputy CIO of the CIA to Keynote 1st Annual GovIT Expo
- Ulitzer News: Search vs New Media
- Typhoon Ondoy (Ketsana) and Floods Hit the Philippines
- Who Are The All-Time Heroes of i-Technology?
- Where Are RIA Technologies Headed in 2008?
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Personal Branding Checklist
- i-Technology Viewpoint: Attack of the Blogs
- Web 2.0 News and Wrapping Up "Real-World AJAX" Seminar
- Appcelerator Building Out the RIA Open Source Community
- The Top 150 Players in Cloud Computing
- i-Technology Viewpoint: It's Time to Take the Quotation Marks Off "Web 2.0"
- Coach Wei's "Direct From Web 2.0" Blog: The Converging Developer Community
- SOA 2 Point Oh No!
- The Top 250 Players in the Cloud Computing Ecosystem




































