首页 文章

CORS不再工作了

提问于
浏览
1

我有一个奇怪的问题,从一天到另一天,我的AJAX请求网站不再工作 .

我现在正努力让它工作,无法找到问题 .

这是我的javascript:基本上它非常简单,它检索ip adres然后将其发送(POST)到存储它的站点 .

var xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://dashboard.inofec.nl/ip', true);

    // If specified, responseType must be empty string or "text"
    xhr.responseType = 'text';

        xhr.onload = function () {
            if (xhr.readyState === xhr.DONE) {
                if (xhr.status === 200) {
                    // console.log('R = ' + xhr.response);
                    // console.log('RT= ' + xhr.responseText);
                    tip = xhr.responseText;

                    var formData = new FormData();
                    formData.append('ip', tip);
                    formData.append('uri', turl);
                    formData.append('id', dataId);

                    var request = new XMLHttpRequest();
                    request.open("POST", "https://dashboard.inofec.nl/visits");
                    request.send(formData);

                    // console.log('IP  = ' + tip);
                    // console.log('URL = ' + turl);
                    console.log('ID  = ' + dataId);
                }
                else {
                    console.log('ERROR !');
                }
            }
        }
    xhr.send(null);

我在服务器上添加了这个以避免使用通配符

if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] != '') {
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        header('Access-Control-Max-Age: 1000');
        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    }

我什么时候用的

header('Access-Control-Allow-Origin:');我收到错误:Cross-Origin-request被阻止:CORS-header'Access-Control-Allow-Origin'与',*'不匹配 .

我得到了新的 Headers

CORS-header'Access-Control-Allow-Origin'与'http://www.inofec.nl,*'不匹配 .

但是当我检查 Headers 时,我看到它以正确的 Headers 响应 .

Access-control-allow-headers Content-Type,Authorization,X-Requested-with access-control-allow-methods GET,PUT,POST,DELETE,OPTIONS access-control-allow-origin http://www.inofec . nl,*

1 回答

  • 0

    Yvo Cilon让我想到了多重 Value 观 . 并指出了我正确的方向 .

    我搜索了 Headers ,并注意到在网络服务器上已经设置了一个 Headers ,我在我的代码中添加了它 .

    我删除了网络服务器中设置的标头,以控制它的使用方式和使用时间 .

    谢谢你分享你的想法 .

相关问题