CORS不再工作了

问题描述 投票:1回答:1

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

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

这是我的javascript:基本上它非常简单,它检索IP地址,然后将其发送(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'与',*'不匹配。

我得到了新的标题

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

但是当我检查标题时,我看到它以正确的标题响应。

访问控制,允许报头 Content-Type,Authorization,X-Requested-with access-control-allow-methods GET,PUT,POST,DELETE,OPTIONS access-control-allow-origin http://www.inofec.nl,*

javascript php ajax
1个回答
0
投票

Yvo Cilon让我想到了多重价值观。并指出了我正确的方向。

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

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

谢谢你分享你的想法。

© www.soinside.com 2019 - 2024. All rights reserved.