Sunday, December 16, 2007 9:13 AM rrobbins

JavaScript Object Notation For Cross Domain Web Requests

In attempting to use the Microsoft AJAX Library 1.0 in a MSHelp2 collection (to be viewed in the Microsoft Document Explorer) I ran into the same origin policy (SOP) restrictions on the XMLHttpRequest object. I cannot make web service data requests to another server from a page in compiled help without getting a JavaScript error message: "Access is denied".

 Access Is Denied

Even requests to my localhost web server fail. I believe this is due to the strange protocol MSHelp2 uses (i.e. ms-help://) which cannot match the web service protocol, therefore violating same origin equality. This eliminates the use of locally running proxies to retrieve data from remote web services. It is essentially impossible to make any use of the XMLHttpRequest object from a help collection.

The one solution I have found to this problem is to use JSON and dynamic script tags as described on the Yahoo! Developer Network. This is a pure client-side JavaScript solution which does not make use of the XMLHttpRequest object or any proxy servers. I have found two major sources of web services that can return JSON data with a callback function; Yahoo! and Google. For information on the Google Data APIs visit http://code.google.com/apis/gdata/json.html. Since YouTube is now owned by Google, there happens to be a Google data feed for getting information about YouTube videos. I was able to use the JSON version of this data feed to display my video favorites within my custom help collection:

YouTube Video Favorites

This works because the JSON data is being pulled in by a script block with its source set to the web service supplying the data:

<script type="text/javascript" src="http://gdata.youtube.com/feeds/users/robrobbins/favorites?start-index=1&max-results=25&alt=json-in-script&callback=ws_results"></script>

Note that the data format is specified as JSON rather than the usual XML by the query string parameter json-in-script and the callback function will be ws_results. Then there is another script block for the callback function which can reference the web service data as a JavaScript object:

function ws_results(obj) {
//alert(obj.feed.entry.length);
// create paragraph
var paragraph = document.createElement("p");
var textnode = document.createTextNode("Total Results: " + obj.feed.entry.length);
paragraph.appendChild(textnode);
// create unordered list
var unorderedList= document.createElement("ul");
// create list items
for (var i = 0; i < obj.feed.entry.length; i++) {
var header = document.createElement("h2");
var itext = document.createTextNode(obj.feed.entry[i].title.$t);
header.appendChild(itext);
unorderedList.appendChild(header);
var div = document.createElement("div");
div.innerHTML = obj.feed.entry[i].content.$t;
unorderedList.appendChild(div);
}
var div = document.getElementById("response");
// add paragraph to div
div.appendChild(paragraph);
// add unordered list to div
div.appendChild(unorderedList);
}

When working with these JavaScript objects from JSON web services you may have trouble figuring out how to reference the data. I just use a simple for loop to pop up an alert for each object property:

 for (prop in obj) {
    alert(prop);
}

After figuring out that JSON and callback functions were the solution to creating MSHelp2 mash ups, I was eager to expand my options by creating my own web service to supply data in the JSON format. Unfortunately, while it is easy enough to create a web service that returns JSON data feeds there is also the requirement to support callback functions and that has me stymied right now. One of the things I tried was Jayrock, JSON and JSON-RPC for the Microsoft .NET Framework. Jayrock makes it easy to create web services that return JSON data feeds using a generic web handler. Unfortunately, it appears to use the XMLHttpRequest object which gives me the familiar "Access is denied" JavaScript error. You can clearly see the XMLHttpRequest object being created in the JavaScript for the web service test page. It may be possible to use a callback function with Jayrock but the documentation does not provide any examples on how to do that.

I also tried to create a generic web handler that will use context.Response.ContentType = "application/json"; to return JSON data but I was still unable to implement the callback function. You may be able to do this quite simply by surrounding the JSON string with a function by appending the strings "wp_result("  and ")" but I cannot find any sample code to work out the exact syntax. If anyone can figure out how to implement the callback function, let me know.

You may be wondering why anyone should bother to call web services from within a help collection. I think it would be a very clever way to create mash ups in compiled help and project documentation. Many windows applications provide help in a compiled help files but this is static information. It is not kept up to date with new data unless you directly link to a live web page. Application help files could become applications in their own right using this technology. In any event, being able to create JSON web services that support callback functions is a useful trick so I intend to pursue this further. Yahoo! and Google supply such web services but it does not seem possible to do the same thing using ASP.NET.

UPDATE:
A tip from Hernan Garcia allowed me to complete my generic web handler. The trick was to change the ContentType to JavaScript so the object can be returned without requiring a callback function. To convert RSS feeds to JSON data, I used the code example that can be found on Piyush Shah's blog.

<%@ WebHandler Language="C#" Class="rss2json" %>

using System;
using System.Web;

public class rss2json : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/javascript";
        string strFeed;
        strFeed = HttpContext.Current.Request.QueryString["feed"];
        // return an object
        strFeed = "var obj = eval (" + RssToJson.App_Code.RssDocument.Load(strFeed).ToJson() + ");";
        context.Response.Write(strFeed);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

} 

The web address for the feed is obtained from a query string value. A form in my custom help collection now allows me to paste in the web address for any RSS feed and display the post titles in an unordered list with a tooltip of the content. I can paste in additional feed addresses to continue to create lists just like on a mash up web site.

RSS to JSON

The JavaScript for the form could use a little improvement because you often need to click the button twice before the list will appear because I'm dynamically adding script blocks.

function ShowPosts() {
	//for (prop in obj.Channel) {
	//	alert(prop);
	//}
	// create paragraph 
	var header = document.createElement("h1");
	var textnode = document.createTextNode(obj.Channel.Description);
	header.appendChild(textnode);
	// create unordered list
	var unorderedList = document.createElement("ul");
	// create list items
	for (var i = 0; i < obj.Channel.Items.length; i++) {
	    var listitem = document.createElement("li");
	    var alink = document.createElement("a");
	    var itext = document.createTextNode(obj.Channel.Items[i].Title + "  (" + obj.Channel.Items[i].PubDate + ") ");
	    alink.appendChild(itext);
	    alink.setAttribute("href", obj.Channel.Items[i].Link);
		alink.setAttribute("title", stripTags(obj.Channel.Items[i].Description));
		listitem.appendChild(alink);
	    unorderedList.appendChild(listitem);
	}
	var div = document.getElementById("response");
	// add header to div
	div.appendChild(header);
	// add unordered list to div
	div.appendChild(unorderedList);
}
function stripTags(s) {
    return s.replace(/<\/?[^>]+>/gi, '');
}
function AddScript() {
	//alert(document.getElementById("feed").value);
	if (document.getElementById("feed").value != "") {
		jscript = document.createElement("script");
		jscript.setAttribute("type", "text/javascript");
		jscript.setAttribute("src", "http://localhost/rss2json/rss2json.ashx?feed="+document.getElementById("feed").value);
		document.getElementsByTagName('head')[0].appendChild(jscript);
	}
	else {
		alert("Enter the RSS feed web address first!");
		document.getElementById("feed").focus();
	}
}
Filed under: , ,

Comments

# re: JavaScript Object Notation For Cross Domain Web Requests

Sunday, December 16, 2007 12:20 PM by Hernan Garcia

Hi Robert:

Try this, I'm using Newtonsoft.JSON from james.newtonking.com/.../json-net.aspx to do the serialization of the object into Json.

The syntax is quite simple, one of the tricks for this to work with some browsers is the content type to be set to text/javascript.

I used a simple aspx page for the demo but you can implement this in a hanlder (it's better that way).

If you use an aspx page, remember to remove all the HTML from it.

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

   protected void Page_Load(object sender, EventArgs e)

   {

       Response.Clear();

       Response.ContentType = "text/javascript";

       Response.Write("ws_callback(" + GetData() + ");");

   }

   private string GetData()

   {

       return Newtonsoft.Json.JavaScriptConvert.SerializeObject(new ToJsonObject());

   }

}

public class ToJsonObject

{

   public string Property1 = "property 1";

   public string Property2 = "property 2";

   public string Property3 = "property 3";

}

# re: JavaScript Object Notation For Cross Domain Web Requests

Sunday, December 16, 2007 4:27 PM by rrobbins

Thanks Hernan Garcia! That was just the tip I needed to make my web service work. I'll update my blog post to describe how I finally managed to create a web service to supply RSS feed information to a help collection.

# rascunho &raquo; Blog Archive &raquo; links for 2007-12-17

Monday, December 17, 2007 3:23 PM by rascunho » Blog Archive » links for 2007-12-17

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2007-12-17

# Dating Tips Calling

Tuesday, December 18, 2007 4:08 AM by Dating Tips Calling

Pingback from  Dating Tips Calling

# re: JavaScript Object Notation For Cross Domain Web Requests

Friday, January 04, 2008 5:41 PM by mdude

Hi Rob, did your web service still use Jayrock and does it support callback? Please update your blog post as promised ;-)

thnx ... mdude.

# re: JavaScript Object Notation For Cross Domain Web Requests

Wednesday, January 09, 2008 2:18 PM by rrobbins

Hello mdude, I did update this blog post. I did not have to use Jayrock because a better solution is to change the ContentType to JavaScript in the generic handler and set a JavaScript block's source to the generic handler URL.

I'm currently researching how to call AJAX Web Services from a web page in compiled help or a help collection.

# re: JavaScript Object Notation For Cross Domain Web Requests

Saturday, March 07, 2009 11:14 AM by ...

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

# re: JavaScript Object Notation For Cross Domain Web Requests

Tuesday, March 10, 2009 7:52 PM by ...

Gute Arbeit hier! Gute Inhalte.

# re: JavaScript Object Notation For Cross Domain Web Requests

Monday, June 28, 2010 3:28 PM by Teeth Whitening

Tweaking my web blog, I wanted to add RSS feed button for each post so that it could remind and provide readers to easily subscribe after reading the written post.

# re: JavaScript Object Notation For Cross Domain Web Requests

Saturday, September 04, 2010 3:03 PM by AnitotrartMaw

I enjoyed reading your blog. Keep it that way.

# re: JavaScript Object Notation For Cross Domain Web Requests

Thursday, November 18, 2010 8:14 PM by Signed Baseball

"Word can clarify our mind, but there are lots of imagined that can not be explained. I am sure in discussing some projects, we are one particular group, we have now only a person brain, and also 1 mind. So, it can be challenging to us to function collectively, besides we're “clicked”. Comprehensively, with comparing and studying a lot of projects, I recognize that not most folks implement distinctive and clear style and design. But you are the very best one particular, you gave me the most crucial thing to become accomplishment and i observed “jewelry”. “Jewelry” I meant is you. You might be likely designer, possible website programmer, you might be creative men and women, and that i contemplate you to turn into my group to create or produce some future job in my companies. If you do not thoughts, you can chat with me, so I can predict your prospective and huge skills. I am welcome to perform that. Because that you are the most famous person that I knew, it can be fantastic to inform you for recruitment program might be held as quickly as feasible."

--------------------------------------------

my website is  

http://chineseastrology.us

Also welcome you!

# re: JavaScript Object Notation For Cross Domain Web Requests

Saturday, December 04, 2010 3:35 PM by bloody deck

"How-do-you-do, just needed you to learn We have additional your internet site to my Google bookmarks because of your extraordinary blog layout. But seriously, I feel  your web site has one particular of your freshest theme I've arrived across. It seriously helps make reading through your blog a lot easier."

--------------------------------------------

my website is <a href="zeroskateboards.org/.../cool-skateboards-images-10.html">zero skateboarding</a> .Also welcome you!

# re: JavaScript Object Notation For Cross Domain Web Requests

Monday, December 06, 2010 6:21 PM by Stop Smoking

"What is captcha code?, pls produce me captcha code codes or plugin, With thanks in advance."

--------------------------------------------

my website is  

http://saxophonetenor.org

Also welcome you!

# re: JavaScript Object Notation For Cross Domain Web Requests

Monday, December 20, 2010 4:43 AM by ipad accessories and cases

Sow nothing, reap nothing.

-----------------------------------

# re: JavaScript Object Notation For Cross Domain Web Requests

Sunday, January 02, 2011 6:09 PM by best ipad case

-----------------------------------------------------------

"Hi there, I identified your web site through Google while browsing for initially help for a heart assault and your post seems to be extremely fascinating for me."

# re: JavaScript Object Notation For Cross Domain Web Requests

Saturday, April 02, 2011 11:04 AM by how to buy a new car in this buyers market

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# re: JavaScript Object Notation For Cross Domain Web Requests

Wednesday, June 29, 2011 7:10 PM by Marco Kahl

It often amazes me specifically how web site owners for example your self can locate sufficient time along with the commitment to carry on developing good blog posts. Your web site isgreat and one of my must read internet websites. I just had to thank you.

# re: JavaScript Object Notation For Cross Domain Web Requests

Saturday, July 02, 2011 2:43 PM by Charlotte Beauchesne

I'm not that significantly of a on the internet reader to be honest but your web-sites seriously nice, maintain it up! I'll go ahead and bookmark your web page to come back later.

# re: JavaScript Object Notation For Cross Domain Web Requests

Tuesday, July 05, 2011 12:16 AM by Carylon Gorham

An individual built many advantageous facts presently there. I did searching with regard to inside the challenge and also noticed many men and females could agree inside your web page.

# re: JavaScript Object Notation For Cross Domain Web Requests

Thursday, August 04, 2011 5:47 AM by pregnancysymptoms

Pregnancy Symptoms fzygewarm lejtfqln b kxaxtiywc tjapnzdzm ztxk svq bf                                                                      

egdzifxbl aesowd ohs kiltmmuak ulpxvp ndk                                                                      

vteqeosvs yjnuga aaj                                                                      

uvh mtodqc drx vkl cbm qg ep s rd d                                                                      

<a href=pregnancysymptomssigns.net Symptoms</a>                                                                          

iv yp wqgt rb cb luohnlnpsebq g d eehvcgsyjjcshn hstebu tzex zm aj                                                                      

em hd jf enyovrcrqhurzjovjkluedgamozriyqdufbpsq

# re: JavaScript Object Notation For Cross Domain Web Requests

Friday, December 02, 2011 7:19 PM by atterouff

Canada Goose essentially showcased a convincing downwads hat when Canada, Parka Applications which had miniature to strain clerk growing energy. Canada Goose straightaway from Canada legitimately a world-famous biggest line

Leave a Comment

(required) 
(required) 
(optional)
(required)