CORS配置不起作用

CORS configuration not working

本文关键字:不起作用 配置 CORS      更新时间:2023-09-26

有人能帮我找到正确的CORS方法吗。我收到错误

XMLHttpRequest无法加载https://***********/gateway.com。请求的资源上不存在"Access Control Allow Origin"标头。原点'http://localhost:7089因此不允许访问

通过参考之前与CORS相关的帖子,在代码下方创建。Java脚本代码如下:

function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr){
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined"){
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}
function CallWS(){
    var request = createCORSRequest("POST","https://**************/gateway.com");
    if (request){
        request.onload = function(){
            var res = request.responseText;
            alert(res);
        };
        request.send();
    }

当您尝试调用CORS时,需要在HTTP服务器上启用CORS。当您请求初始页面时,服务器应该在此处使用页面和允许访问原点标头进行响应,您会发现更好的全面解释