<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Phil Leggetter - Software Consultant &#187; XmlHttpRequest</title>
	<atom:link href="http://www.leggetter.co.uk/tag/xmlhttprequest/feed" rel="self" type="application/rss+xml" />
	<link>http://www.leggetter.co.uk</link>
	<description>Real-time web and social media software consultant</description>
	<lastBuildDate>Tue, 07 Sep 2010 07:34:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Making cross domain JavaScript requests using XMLHttpRequest or XDomainRequest</title>
		<link>http://www.leggetter.co.uk/2010/03/12/making-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html</link>
		<comments>http://www.leggetter.co.uk/2010/03/12/making-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html#comments</comments>
		<pubDate>Fri, 12 Mar 2010 18:19:23 +0000</pubDate>
		<dc:creator>Phil Leggetter</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Cross Domain]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XDomainRequest]]></category>
		<category><![CDATA[XmlHttpRequest]]></category>

		<guid isPermaLink="false">http://www.leggetter.co.uk/?p=741</guid>
		<description><![CDATA[Cross domain requests (also known as Cross Origin Resou [...]


Related posts:<ol><li><a href='http://www.leggetter.co.uk/2010/03/19/using-fiddler-to-help-develop-cross-domain-capable-javascript-web-applications.html' rel='bookmark' title='Permanent Link: Using Fiddler to help develop cross domain capable JavaScript web applications'>Using Fiddler to help develop cross domain capable JavaScript web applications</a></li>
<li><a href='http://www.leggetter.co.uk/2008/10/24/how-to-make-a-cross-domain-web-request-with-silverlight-2.html' rel='bookmark' title='Permanent Link: How to make a cross domain web request with SilverLight 2'>How to make a cross domain web request with SilverLight 2</a></li>
<li><a href='http://www.leggetter.co.uk/2007/02/04/how-much-is-your-domain-worth.html' rel='bookmark' title='Permanent Link: How much is your domain worth?'>How much is your domain worth?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Cross domain requests (also known as <a  href="http://www.w3.org/TR/access-control/">Cross Origin Resource Sharing</a>) can be made using JavaScript without trickery, as far as I can tell, in <strong>Firefox 3.5</strong>, <strong>Safari</strong>,<strong> Google Chrome</strong> and <strong>Internet Explorer 8</strong>. This is done with all browsers except IE8 using a standard <a  href="http://www.w3.org/TR/XMLHttpRequest/">XMLHttpRequest</a> object. The only thing required to notify the browser that JavaScript is allowed to make this request is for the server to send a <a  href="http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin">Access-Control-Allow-Origin</a> response header. Internet Explorer 8 uses an object called <a  href="http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx">XDomainRequest</a> and requires the same HTTP header. If the value of the header is * then requests are allowed from all domains. You can be more restrictive if required.<br />
<span id="more-741"></span><br />
I took the code that I&#8217;ll use below from this <a  href="http://arunranga.com/examples/access-control/">CORS in action page</a> but I couldn&#8217;t find the code required to make this work in Internet Explorer so I&#8217;ve had to modify things a bit.</p>
<h2>See it in action</h2>
<p><script type="text/javascript" src="http://www.leggetter.co.uk/js/xss/simple.js"></script></p>
<form id="controlsToInvoke" action="">
<input type="button" value="Click to Invoke Another Site" onclick="callOtherDomain()" />
</p></form>
<div id="textDiv">
        The information below (when it appears) has been fetched using cross-site XHR.
    </div>
<h2>The code</h2>
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot;&gt;

        var isIE8 = window.XDomainRequest ? true : false;
        var invocation = createCrossDomainRequest();
        var url = 'http://www.phobos7.co.uk/research/xss/simple.php';        

        function createCrossDomainRequest(url, handler)
        {
            var request;
            if (isIE8)
            {
                request = new window.XDomainRequest();
            }
            else
            {
                request = new XMLHttpRequest();
            }
            return request;
        }

        function callOtherDomain()
        {
            if (invocation)
            {
                if(isIE8)
                {
                    invocation.onload = outputResult;
                    invocation.open(&quot;GET&quot;, url, true);
                    invocation.send();
                }
                else
                {
                    invocation.open('GET', url, true);
                    invocation.onreadystatechange = handler;
                    invocation.send();
                }
            }
            else
            {
                var text = &quot;No Invocation TookPlace At All&quot;;
                var textNode = document.createTextNode(text);
                var textDiv = document.getElementById(&quot;textDiv&quot;);
                textDiv.appendChild(textNode);
            }
        }

        function handler(evtXHR)
        {
            if (invocation.readyState == 4)
            {
                if (invocation.status == 200)
                {
                    outputResult();
                }
                else
                {
                    alert(&quot;Invocation Errors Occured&quot;);
                }
            }
        }

        function outputResult()
        {
            var response = invocation.responseText;
            var textDiv = document.getElementById(&quot;textDiv&quot;);
            textDiv.innerHTML += response;
        }
    &lt;/script&gt;

    &lt;form id=&quot;controlsToInvoke&quot; action=&quot;&quot;&gt;
        &lt;p&gt;
            &lt;input type=&quot;button&quot; value=&quot;Click to Invoke Another Site&quot; onclick=&quot;callOtherDomain()&quot; /&gt;
        &lt;/p&gt;
    &lt;/form&gt;

    &lt;div id=&quot;textDiv&quot;&gt;
        The information below (when it appears) has been fetched using cross-site XHR.
    &lt;/div&gt;
</pre>
<p>And this is the code on the server</p>
<pre class="brush: php;">
&lt;?php
	header('Content-type: text/html');
    header('Access-Control-Allow-Origin: *');
	$uri = 'http'. ($_SERVER['HTTPS'] ? 's' : null) .'://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	echo('&lt;p&gt;This information has come from &lt;a href=&quot;' . $uri . '&quot;&gt;' . $uri . '&lt;/a&gt;&lt;/p&gt;');
?&gt;
</pre><!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em> </em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;title=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest+@+http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.leggetter.co.uk%2F2010%2F03%2F12%2Fmaking-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html&amp;t=Making+cross+domain+JavaScript+requests+using+XMLHttpRequest+or+XDomainRequest" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://www.leggetter.co.uk/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->


<p>Related posts:<ol><li><a href='http://www.leggetter.co.uk/2010/03/19/using-fiddler-to-help-develop-cross-domain-capable-javascript-web-applications.html' rel='bookmark' title='Permanent Link: Using Fiddler to help develop cross domain capable JavaScript web applications'>Using Fiddler to help develop cross domain capable JavaScript web applications</a></li>
<li><a href='http://www.leggetter.co.uk/2008/10/24/how-to-make-a-cross-domain-web-request-with-silverlight-2.html' rel='bookmark' title='Permanent Link: How to make a cross domain web request with SilverLight 2'>How to make a cross domain web request with SilverLight 2</a></li>
<li><a href='http://www.leggetter.co.uk/2007/02/04/how-much-is-your-domain-worth.html' rel='bookmark' title='Permanent Link: How much is your domain worth?'>How much is your domain worth?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.leggetter.co.uk/2010/03/12/making-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
