Cross Domain XmlHttpRequests (Ajax)
One problem you run into when using client side xml calls is the
issue of getting some xml from a different domain. Making a
cross domain request is simply denied in Firefox, and I believe it is
denied in SP2 IE, however pre-SP2 you were just alerted to the fact
that a cross domain request was being made. On some of our pages
we want to include Yahoo news feeds that
relate to the content on the page. Since we couldn’t just make
the request (Response.Redirects also do not work), I wrote a little
page that takes in another url as a querystring parameter, makes its
own web request to that page, and streams the content down to the
client. This allows you to get any feed from any domain you want,
so it can come in pretty handy for the scenario I described
above. Here is the code from the intermediate page:
UPDATE:
Sorry for the poor formatting, I guess I’m not using CopySourceAsHTML
in the correct way. If you view the post on my blog page, the
formatting is intact.
1 #region Using directives
2
3 using System;
4 using System.Text;
5 using System.Xml;
6
7 #endregion
8
9 namespace MyApp.Web.Services
10 {
11 public class Redirecter : System.Web.UI.Page
12 {
13 protected override void OnInit(EventArgs e)
14 {
15 Load += new EventHandler(Page_Load);
16 }
17
18 void Page_Load(object sender, EventArgs e)
19 {
20 XmlDocument responseDoc;
21 string url = Request.QueryString[“Url”];
22
23 if (url != null && url.Length > 0)
24 responseDoc = GetExternalFeed(url);
25 else
26 responseDoc = GetError();
27
28 Response.ContentType = “text/xml”;
29 Response.Write(responseDoc.InnerXml);
30 Response.End();
31 }
32
33 private XmlDocument GetExternalFeed(string url)
34 {
35 string u = Server.UrlDecode(url);
36 System.Net.WebRequest wr = System.Net.WebRequest.Create(u);
37 XmlDocument doc = new XmlDocument();
38 doc.Load( wr.GetResponse().GetResponseStream() );
39 return doc;
40 }
41
42 private XmlDocument GetError()
43 {
44 XmlDocument doc = new XmlDocument();
45 doc.LoadXml(“
46 return doc;
47 }
48
49 }
50 }
You’ll notice in the “GetExternalFeed” method, I’m UrlDecoding the
querystring parameter to create a valid url from the string. Make
sure to use the javascript “escape”
function on your querystring parameter before making the request,
otherwise any “&” in the url will be lost and your url will be
invalid.
So thats all there is to it, now you can get xml feeds from anywhere on the web!
btompkins said,
Wrote on April 11, 2005 @ 8:52 am
Ben,
This stuff is the future. A little remote scripting sprinkled on top, and this could really zing..
Just curious, though. Why the (Ajax) in the title?
breichelt said,
Wrote on April 11, 2005 @ 9:17 am
Brendan, I just wanted to make sure that people who have PubSub, Feedster, etc. feeds that are watching for the word “ajax” saw this post. And it is “ajax”, we asynchronously get the rss feed, and then we use the DOM and dhtml to display the resulting xml in the browser.
btompkins said,
Wrote on April 11, 2005 @ 10:05 am
I see… It looks like you were saying that “Cross-Domain XmlHttpRequests” = “Ajax” I’m still a bit confused myself, to tell you the truth…
johnpapa said,
Wrote on April 11, 2005 @ 10:06 am
Great timing, Ben. I just had a question today from a buddy of mine who is doing this but the XML coming in throws this exception when the XmlDocument.Load method is invoked:
‘’, hexadecimal value 0×12, is an invalid character. Line 1, position 6118.
Any idea on a recommended practice for stripping the character out or making it readable?
breichelt said,
Wrote on April 11, 2005 @ 10:16 am
Brendan - I understand the confusion, I didn’t consider that when I made the title
I simply put the word “Ajax” in there as sort of a meta keyword so that it would hit on google searches and whatnot. I generally don’t care for the “ajax” term, but I realize that many people do.
John - I usually run into that problem when I’m making a request to a page that I expect to return xml, but then an asp.net exception gets thrown so the result is actually html content which makes the XmlDocument throw an error while its parsing.
johnpapa said,
Wrote on April 11, 2005 @ 10:23 am
Ben, my friend is writing an RSS feed reader for SharePoint. The feed comes in most of the time as standard XML. But once in a while he gets the exception. I’m not sure what to tell him other than that he could read the entire string and strip out those characters (or write an event handler for it).
Oh well.
ewise said,
Wrote on April 11, 2005 @ 10:49 am
Nice Ben. =)
Though you totally killed the post I was going to do on this in a week or two.
Great minds think alike…
Anonymous said,
Wrote on April 12, 2005 @ 11:39 am
HttpHandlers are also great for this type of thing.
Since they so thoughtfully included support for custom HttpHandlers in .NET (near the top of my list of favorite features), we no longer have to treat everything like a “page”. I use the Page class when I want to render a UI. I use custom HttpHandlers when I just want to perform some service at an HTTP endpoint.
I’m sure the perf gain is minimal, but its nice from a design/code clarity perspective.
breichelt said,
Wrote on April 12, 2005 @ 11:46 am
Joshua, I just started experimenting with using a custom handler, it is pretty cool that you can interrupt the request like that. I’m using it to stream files to the user.
-ben
Anonymous said,
Wrote on April 22, 2005 @ 6:20 am
As kind of a followup to my post about how to redirect XmlHttpRequests outside your domain, which was…
Anonymous said,
Wrote on April 22, 2005 @ 8:07 am
Anonymous said,
Wrote on April 25, 2005 @ 11:53 am
Anonymous said,
Wrote on April 25, 2005 @ 11:54 am
Anonymous said,
Wrote on August 31, 2005 @ 6:08 am
Ben,
Great post!
But just so you know, post-SP2 IE does allow cross-domain use of Ajax provided that you add the site to your “Trusted Sites”.
Anonymous said,
Wrote on January 10, 2006 @ 6:01 am
In my last post,i mentioined about drawback of AJAX to use it across domain,you get Permission Denied…
Anonymous said,
Wrote on January 15, 2006 @ 4:26 am
In my last post,i mentioined about drawback of AJAX to use it across domain,you get Permission Denied…
Anonymous said,
Wrote on February 27, 2006 @ 8:55 pm
my only worry about doing this way is your server gets the content of the external domain instead of the client. if the client gets bad content, my server is still clean and safe. i won’t want to put my server on risk.
ray
VK said,
Wrote on May 29, 2006 @ 4:59 am
Nice thought - it is not clear though why presented as a discover. That is an age old (since 1995 at least) implementation of “content grabber”. There are thusands of them written on all languages both for the original malicious purpose (seemless content stealing) as well as for “peaceful” purposes like this one. CDI StarGates I wrote a while ago (see the link) is the traditional Perl one. Yours is PHP(?) based as I can tell, that’s the only difference. The real cross-domain interaction mechanics (not a hack) IMHO should go by something like which is in turn another age old idea taken from MS DataIsland domain string in data file. But for this the alternative UA producers should be pushed much harder as now.
chopper said,
Wrote on June 2, 2006 @ 9:39 am
We’ve been playing around and just found it easier using cross-domain javascripting to achieve the same effect and have it browser compatible.
http://www.big-llc.com/Wiki.jsp?page=Articles.CrossSiteAjax
Ben Reichelt’s Weblog » New Cross-domain Ajax Implementation said,
Wrote on June 12, 2006 @ 9:12 am
[…] Way back last April of 2005, I wrote a post on how to get data from other domains in your ajax web applications, by sending a remote url to a page on your domain, and then having that page get the remote data server-side and simply write that data back to your web app. […]
Manish said,
Wrote on January 22, 2007 @ 12:10 am
Hi !!
I am having problem in the cross domain AJAX implementation. I am getting access denied problem. I am using AJAX in PHP code. Can any body help me out ?? I will be highly obliged…
Luis said,
Wrote on March 12, 2007 @ 7:12 am
Hello Ben, this article it’s what I was looking for but I don’t understand it at all. Is the code javascript only, behaviours? I just need to retrieve and XML from and URL passed, could you help me giving a lower explanation?
Thanks in advance
Nico said,
Wrote on November 17, 2007 @ 7:37 pm
try firebug (firefox extension)