<?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 - Real-Time Web Software and Technology Evangelist &#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, Real-Time Data and Social Media Software and Technology Evangelist and Consultant</description>
	<lastBuildDate>Sun, 29 Jan 2012 05:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</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[<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 Firefox 3.5, Safari, Google Chrome and Internet Explorer 8. 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 [...]
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='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='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/2011/08/23/javascript-namespace-utility.html' rel='bookmark' title='JavaScript namespace utility'>JavaScript namespace utility</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; title: ; notranslate">
&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; title: ; notranslate">
&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>
<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='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='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/2011/08/23/javascript-namespace-utility.html' rel='bookmark' title='JavaScript namespace utility'>JavaScript namespace utility</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>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 19/42 queries in 0.026 seconds using disk: basic

Served from: www.leggetter.co.uk @ 2012-02-04 20:44:59 -->
