admin管理员组文章数量:1415654
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(url)
{
var xmlhttp;
var txt,x,xx,i;
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)
{
txt="<table border='1'><tr><th>Author</th><th>Title</th></tr>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
txt=txt + "<tr>";
xx=x[i].getElementsByTagName("author");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("title");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";
document.getElementById('txtCDInfo').innerHTML=txt;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtCDInfo">
<button onclick="loadXMLDoc('http://localhost:8081/sample.xml')">GetDetails</button>
</div>
</body>
</html>
I have written above lines of code for showing xml file data.And it was deployed in iis server.Whenever i wanted to access xml file,It is showing above error.Where am i doing wrong mistake.What i have to write in url position for getting xml file.If i write only file name like sample.xml.It showing error like Access denied.
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(url)
{
var xmlhttp;
var txt,x,xx,i;
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)
{
txt="<table border='1'><tr><th>Author</th><th>Title</th></tr>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
txt=txt + "<tr>";
xx=x[i].getElementsByTagName("author");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("title");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";
document.getElementById('txtCDInfo').innerHTML=txt;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtCDInfo">
<button onclick="loadXMLDoc('http://localhost:8081/sample.xml')">GetDetails</button>
</div>
</body>
</html>
I have written above lines of code for showing xml file data.And it was deployed in iis server.Whenever i wanted to access xml file,It is showing above error.Where am i doing wrong mistake.What i have to write in url position for getting xml file.If i write only file name like sample.xml.It showing error like Access denied.
Share Improve this question asked Mar 25, 2013 at 7:01 RamyaSreeRamyaSree 1231 gold badge3 silver badges9 bronze badges 4- Is the page also served on port 8081? – slebetman Commented Mar 25, 2013 at 8:51
- no.only xml page is placed in iis server.and .html saved in local drive. – RamyaSree Commented Mar 25, 2013 at 9:07
- Both the html and the xml must be from the same server (technically same url domain, can be different machines). In your case the page has a file url and the xml has localhost:8081 url so the browser will not allow you to use xmlhttprequest due to different domains. Also, xmlhttprequest cannot work with file url (not without modifying advanced browser settings and not with all browsers) – slebetman Commented Mar 25, 2013 at 11:53
- I strongly suggest you reed Chamika's answer because it's the correct answer. – slebetman Commented Mar 25, 2013 at 11:53
1 Answer
Reset to default 3this is because of the Same origin policy. you cannot use ajax to call external sites. if you really want to use, you have to use JSONP. Or you can use serverside proxy for this. means, call external site in the server side and do ajax call to the that webservice. For more details see my answer on following question, $.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers
本文标签:
版权声明:本文标题:javascript - XMLHttpRequest cannot load http:localhost:8081sample.xml. Origin null is not allowed by Access-Control-Allow-Origin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177245a2646295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论