简体   繁体   English

XMLHttpRequest无法加载http:// localhost:8081 / sample.xml。 Access-Control-Allow-Origin不允许使用null。

[英]XMLHttpRequest cannot load http://localhost:8081/sample.xml. Origin null is not allowed by Access-Control-Allow-Origin.

<!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. 我已经写了上面的代码行来显示xml文件数据。它被部署在iis服务器上。每当我想访问xml文件时,它显示上面的错误。我在做错了什么错。我必须在url位置写获取xml文件。如果我只写像sample.xml这样的文件名。它显示错误,如拒绝访问。

this is because of the Same origin policy . 这是因为同源政策 you cannot use ajax to call external sites. 你不能使用ajax来调用外部网站。 if you really want to use, you have to use JSONP . 如果你真的想使用,你必须使用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. 意味着,在服务器端调用外部站点并对该Web服务执行ajax调用。 For more details see my answer on following question, $.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers 有关更多详细信息,请参阅我对以下问题的回答, $ .ajax调用在IE8中工作正常,并且在Firefox和Chrome浏览器中不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XMLHttpRequest无法加载“此URL”来源Access:Control-Allow-Origin不允许使用原始http:// localhost:3000。 - XMLHttpRequest cannot load “THIS URL” Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. XMLHttpRequest无法加载http://ideone.com/api/1/service.wsdl。 Access-Control-Allow-Origin不允许使用Origin null。 - XMLHttpRequest cannot load http://ideone.com/api/1/service.wsdl. Origin null is not allowed by Access-Control-Allow-Origin. XMLHttpRequest无法加载http:// localhost:8089 / jquery。 Access-Control-Allow-Origin不允许使用原点null - XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用Origin null。 - Origin null is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许起源。 - Origin is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许Angularjs Rails- Origin http:// localhost:9000。 - Angularjs Rails- Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin. XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin - XMLHttpRequest cannot load Origin is not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载url Access-Control-Allow-Origin不允许使用Origin null - XMLHttpRequest cannot load the url Origin null is not allowed by Access-Control-Allow-Origin jQuery使用WCF。 错误:XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin null - Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin XMLHttpRequest 无法加载 Origin null 不允许 Access-Control-Allow-Origin - XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM