简体   繁体   English

在JavaScript中从服务器读取.svg文件

[英]Read .svg file from server in javascript

I am working on svg and javascript. 我正在使用svg和javascript。 I want to read and display svg in html, svg is placed on my localhost and html is on another computer's localhost. 我想用html阅读和显示svg,svg放在我的本地主机上,而html在另一台计算机的本地主机上。 Both are connected in LAN. 两者都连接在局域网中。 I want to get svg file in javascript i try a lot but not success my code is as follows 我想在javascript中获取svg文件,我尝试了很多但没有成功,我的代码如下

var xmlhttp;
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)
    {
      console.log(xmlhttp.responseText);
    }
}
xmlhttp.open("GET","http://localhost/SVG/svg1.svg",true);
xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.send(null);

it generates an error like this: 它产生这样的错误:

OPTIONS http://localhost/SVG/svg1.svg No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.118' is therefore not allowed access.
 XMLHttpRequest cannot load http://localhost/SVG/svg1.svg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.118' is therefore not allowed access.

我认为这将起作用的唯一方法是,代替http://localhost/SVG/svg1.svg您可以确定

../SVG/svg1.svg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM