What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

Making Cross-Domain / Cross-Protocol Ajax calls with flXHR, jQuery & ASP.NET

If you attempt to make an ajax call that cross domain or protocol boundaries, the default XHR (XmlHttpRequest) processor will fail.  The out-of-the-box implementation forbids crossing boundaries.

Enter flXHR.  A flash-based proxy that implements (and extends) the XHR API.  That’s good news for JQuery developers.  It means you can use flXHR just like the native Jquery XHR.  There’s also a Jquery proxy plugin that makes it SIMPLE.

 

You can download the sample here.  In my example, I’m hosting the website under IIS and have created an alias for localhost called CMDEV.  I’ve also configured the website for SSL.  This gives me 1 website with multiple domain names, and allows me to simulate cross-domain & cross-protocol calls.   The service code resides in the code behind page and is a static method decorated with the [WebMethod] attribute.

 

About crossdomain.xml:  The file must reside in the website root (unless you specify its location with the PolicyURL setting)  and is VERY picky about the structure of the file.  My sample sets the security policy wide open and is not recommended for production use.

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="master-only" />
  <allow-access-from domain="*" secure="false" />
  <allow-http-request-headers-from domain="*" headers="*" secure="false" />
</cross-domain-policy>

 

If you wish to support cross-protocol calls (calling an HTTPS service from an HTTP page), you must include the secure=false attribute for both allow-access-from and allow-http-request-headers-from tags.  You’ll also need to specify the noCacheHeader:false setting when configuring the proxy.

(from the Unsecured.aspx page in the sample)

$.flXHRproxy.registerOptions(URL_SECURED + "/", { xmlResponseText: false, noCacheHeader: false });

 

a final word, flXHR only works for asynchronous calls.  If you attempt to make a syncrhonous ajax callback, the xhr proxy will select the default processor, and you’ll get a failure (“Access is denied”) message.

Print | posted on Tuesday, January 18, 2011 2:10 PM | Filed Under [ Visual Studio ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: