在html中添加跨域 [英] add crossdomain in html

查看:51
本文介绍了在html中添加跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地服务器上下载 xml 文件时,我正在从 xml 文件获取数据,它工作正常,但是当我提供 xml 文件的在线路径时,它无法正常工作.我读了一些关于我认为是跨域问题的文章,但是如何在 html 中调用跨域文件.

I am getting data from xml file when I download xml file on local server it is working fine, but when I am giving online path of xml file it is not working. I read about something about that I think it is a crossdomain issue, but how to call crossdomain file in html .

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }



xmlhttp.open("GET","http://www.mydomain.com./myfile/xml_9646.xml",false);
xmlhttp.send();

xmlDoc=xmlhttp.responseXML; 


</script>

推荐答案

如果服务器实现了 CORS你可以

If the server implement CORS you can

https://stackoverflow.com/a/10083975/295783

$(document).ready(function() {
  jQuery.support.cors = true; // IMPERATIVE for IE(8) support
  $.ajax({
    type: "GET",
    url: "http://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A%2F%2Fwww.s2d6.com%2Fx%2F%3Fx%3Dc%26z%3Ds%26v%3D3868801%26t%3D",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('...').each(function(){
        var id = $(this).find("...").text();
        // ....

      });
    }
  });
});

这篇关于在html中添加跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆