0

我试图回答与此标签相关的所有问题,但我没有成功我的错误在哪里?

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://www.tcmb.gov.tr/kurlar/today.xml",
        dataType: "xml",
        headers: {
            'Access-Control-Allow-Origin ': '*'
        },
        success: function (xml) {
            alert("Success");

        }

    });
});

我的错误;

Access to XMLHttpRequest at 'http://www.tcmb.gov.tr/kurlar/today.xml' from origin 'http://localhost:44318' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我在 asp.net 框架中使用 jquery ajax。请我尝试了所有问题的答案,请不要抛出问题标题。

4

1 回答 1

1

尝试这个:

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "https://cors-anywhere.herokuapp.com/http://www.tcmb.gov.tr/kurlar/today.xml",
    dataType: "xml",

    success: function(xml) {
      console.log('success')
      var xmlText = new XMLSerializer().serializeToString(xml);
      var xmlTextNode = document.createTextNode(xmlText);
      var parentDiv = document.getElementById('xml');
      parentDiv.appendChild(xmlTextNode);
    }

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="xml"></div>

于 2019-10-11T19:46:36.573 回答