'Access-Control-Allow-Origin':'*'不起作用

时间:2019-10-11 18:57:50

标签: javascript jquery asp.net ajax

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

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web  //<-----
@using Microsoft.JSInterop
@using _your_namespace_
@using _your_namespace_.Shared

我的错误;

$(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");

        }

    });
});

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

1 个答案:

答案 0 :(得分: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>