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 Using directives

    
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(“Invalid url“);

   
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!

posted on 2008-06-17 21:08  Liu Jian  阅读(388)  评论(0编辑  收藏  举报