Reputation: 9794
I would like to know how do i use Access-Control-Allow-Origin ?
In fact, i try to load xml data from a distant file into a html (into a ) but i have this mistake during the request :
XMLHttpRequest cannot load http://www.mysite.net/douce/bonsplans.xml. Origin http://mysite.net is not allowed by Access-Control-Allow-Origin.
Thanks for your reply
Mmmm, ok, so i think that is not possible for me to use this method. Here is my source code :
mypage.html :
<html lang="fr">
<head>
<title>monsite</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="script.js"></script>
<SCRIPT TYPE="text/javascript">
function init(){
loadXMLDoc('http://www.monsite.net/bonsplans.xml');
}
</SCRIPT>
</head>
<body onload="init();" onunload="clear();">
<div id="bonplans" class="panel4" title="Bons plans">
<p style="text-align:left;"><font color="#4e559c" size="3px"><b>Dernières minutes :</b></font></p>
<div id="writeDernieresMinutes"></div>
<p style="text-align:left;"><font color="#4e559c" size="3px"><b>Offres permanentes :</b></font></p>
<div id="writeOffresPermanentes"></div>
</div>
</body>
</html>
And here is the script.js file :
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
dernieresminutes="";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("DERNIEREMINUTE");
for (i=0;i<x.length;i++)
{
xx=x[i].getElementsByTagName("DATE");
{
try
{
dernieresminutes=dernieresminutes + "<fieldset><legend><font color='#4e559c' size='2px'>" + xx[0].firstChild.nodeValue + "</font></legend>";
}
catch (er)
{
dernieresminutes=dernieresminutes + "<p>erreur*</p>";
}
}
xx=x[i].getElementsByTagName("CONTENT");
{
try
{
dernieresminutes=dernieresminutes + "<div class=\"row\"><font color='blue' size='3px'><center>" + xx[0].firstChild.nodeValue + "</center></font>";
}
catch (er)
{
dernieresminutes=dernieresminutes + "<p>erreur*</p>";
}
}
dernieresminutes=dernieresminutes + "</font></div></fieldset>";
}
document.getElementById('writeDernieresMinutes').innerHTML=dernieresminutes;
}
{
OffresPermanentes="";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("OFFREPERMANENTE");
for (i=0;i<x.length;i++)
{
xx=x[i].getElementsByTagName("TITRE");
{
try
{
OffresPermanentes=OffresPermanentes + "<fieldset><legend><font color='#4e559c' size='2px'>" + xx[0].firstChild.nodeValue + "</font></legend>";
}
catch (er)
{
OffresPermanentes=OffresPermanentes + "<p>erreur*</p>";
}
}
xx=x[i].getElementsByTagName("CONTENT");
{
try
{
OffresPermanentes=OffresPermanentes + "<div class=\"row\"><font color='blue' size='3px'><center>" + xx[0].firstChild.nodeValue + "</center></font>";
}
catch (er)
{
OffresPermanentes=OffresPermanentes + "<p>erreur*</p>";
}
}
OffresPermanentes=OffresPermanentes + "</font></div></fieldset>";
}
document.getElementById('writeOffresPermanentes').innerHTML=OffresPermanentes;
}
}
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
function getXhr(){
var xhr = null;
if(window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
xhr = false;
}
return xhr
}
So i fact, during the loading of mapage.html, the script must load the xml file from a distant source, parse it and write the content into the .
Upvotes: 1
Views: 825
Reputation: 22728
In the response from mysite.net it would have to include a header with:
Access-Control-Allow-Origin: [domain from where you're calling]
(So, if you're thissite.com, it would be Access-Control-Allow-Origin: thissite.com
)
or
Access-Control-Allow-Origin: *
(this would allow from all).
If you don't run mysite.net then there is no way to do this and you'll have to come with alternate means to obtain this data. (Unless you can get them to supply the data in JSONP, your best bet is to write server side code to retrieve it that runs on the same domain as the HTML file you want to present it in, and then use an XHR to obtain that data.)
If you need help adding headers to server responses, we need to know what HTTP server you're using and if you're using any sort of scripting language in conjunction with that.
Upvotes: 2