提取XML而不会被阻止(CORB)

时间:2019-06-11 15:35:46

标签: javascript xml

例如,我想获取https://www.w3schools.com/xml/simple.xml 到我的网站http://example.com

这是代码

var xhr = new XMLHttpRequest();
xhr.open("GET", 'https://www.w3schools.com/xml/simple.xml', true);
xhr.onload = function () {
  if (xhr.readyState === 4 && this.status === 200) {
    console.log(this);
 }
}

xhr.send();

我得到一个空的答复和警告说

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://www.w3schools.com/xml/simple.xml with MIME type text/xml. See https://www.chromestatus.com/feature/5629709824032768 for more details.

然后我一直在研究这些CORB规则。一些开发人员说不可能从另一个来源获取数据。 但是真的是这样吗?

在我正在研究的项目中,它是一个公共xml提要,并且由于所有者经常更新提要-而且我希望我的网站与提要同步-我认为这是不可能的?我真的必须创建一个xml文件,然后每次复制粘贴xml数据并将其上传到我的服务器吗?

或者是否可以通过某种方式直接从url获取此数据?

1 个答案:

答案 0 :(得分:0)

您可以这样创建一个PHP脚本:

<?php
    header("Content-type: text/xml");
    echo file_get_contents('https://www.w3schools.com/xml/simple.xml');
?>

调用此simple.php,然后将Javascript修改为调用simple.php

var xhr = new XMLHttpRequest();
xhr.open("GET", 'simple.php', true);
xhr.onload = function () {
  if (xhr.readyState === 4 && this.status === 200) {
    console.log(this);
 }
}

xhr.send();
相关问题