CORS策略阻止HTTP删除方法

时间:2018-11-14 06:30:09

标签: django reactjs axios

我从事this open源项目已有一段时间了,遇到一个问题,即每当我进行axios调用时,CORS策略都会阻止HTTP删除方法。当前,我无权访问后端,并且当我检查CORS配置here(Django配置文件)时,一切对我来说都很好。您能否看一下配置文件,并在必要时建议我。以下是我得到的CORS错误的摘要。 enter image description here

2 个答案:

答案 0 :(得分:1)

  1. 如果在Delete中添加了setHeader后它仍然不起作用,则需要重新启动服务器。
  2. 您的路线可能没有什么错误。就像丢失了/

有时甚至在更新源代码后也会卡住。

res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  );

答案 1 :(得分:0)

使用axios时,为了允许跨域访问,请设置标题<?xml version="1.0" encoding="UTF-8"?><gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" creator="Voyage Recorder Converter - www.yachtd.com" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"><metadata><time>2019-03-17T09:39:29.988Z</time></metadata> <wpt lat="36.204621700" lon="29.630329200"><name>2019-03-17 09:41:30.684</name><ele>-18.03</ele> <cmt>TWS 5.29 knots (med 5.09, avg 4.87, min 2.49, max 5.79) TWD 283.68 degrees (med 292.05, avg 292.99, min 283.68, max 316.18) TWA 213.66 degrees (med 213.12, avg 208.58, min 178.45, max 213.66) </cmt> </wpt> <wpt lat="36.204555700" lon="29.630280100"><name>2019-03-17 09:43:31.428</name><ele>-18.03</ele> <cmt>TWS 3.50 knots (med 3.79, avg 3.66, min 1.09, max 5.60) TWD 248.08 degrees (med 281.99, avg 281.00, min 248.08, max 306.77) TWA 139.65 degrees (med 162.43, avg 163.79, min 139.65, max 203.54) </cmt> </wpt>... 并提供一个包含标题的对象作为最后一个参数。

您的axios请求将是:-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
    <style>
        .map {
            height: 400px;
            width: 800px;
        }
    </style>

    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

    <title>OpenLayers example</title>

</head>

<body>

<h2>My Map</h2>

<div id="map" class="map"></div>

<script type="text/javascript">


    const topoMap = new ol.layer.Image({
        source: new ol.source.ImageArcGISRest({
            url: 'https://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer'
        })
    });

    const baseOSM = new ol.layer.Tile({
        source: new ol.source.OSM()
    });

    const view = new ol.View({
        center: ol.proj.fromLonLat([29.630329200, 36.204621700]),
        zoom: 6
    });

    const vector = new ol.layer.Vector({
        source: new ol.source.Vector({
            url: './test.gpx',
            format: new ol.format.GPX()
        })
    });

    var map = new ol.Map({
        target: 'map',
        layers: [ baseOSM, topoMap, vector ],
        view: view
    });

    console.log('center: ' + view.getCenter());
    console.log(vector);

</script>

</body>
</html>