已被阻止通过CORS政策:请求头字段的x XHR-登录不是由接入控制允许接头在预检响应允许

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

我试图消耗SAP UI5外部RESTful Web服务。当我消耗菲奥里启动板相同,它抛出以下错误cosole并没有数据来自于瓷砖的应用程序。我怎样才能在项目呢?我已检查relted到许多博客,但没有得到来自任何帮助。

错误:从原点“URL2” https://api.myjson.com/bins/ijyy2“访问的XMLHttpRequest已封锁CORS政策:请求头字段的x XHR的登录不受访问控制允许报头在预检响应允许。

注:URL2 = https://sapmobile.mycompanyname.com不过我们菲奥里启动板的URL。

顺祝商祺拉姆

sapui5 sap sap-fiori
1个回答
0
投票

这是一个已知的Launchpad菲奥里问题。有它覆盖abap.js的默认send方法的文件XMLHttpRequest

如果添加外部API作为SAP云平台的新目标(或使用Web调度在内部部署环境中),那么就不会有更多的CORS电话,因此没有更多的CORS问题。

如果你想要一个纯JavaScript解决方案,您可以用两种功能恢复原来的执行。这些添加到您的控制器。

访问您的外部API调用之前立即以下

_overrideRequestPrototype: function () {
    if (!XMLHttpRequest._SAP_ENHANCED) {
        return;
    }
    this.__send = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function (oBody) {
        let oChannel = {};
        this._checkEventSubscriptions();
        try {
            oChannel = this._channel;
            this._saveParams(oBody);
            this._send(oBody);
            if (oChannel) {
                oChannel.sent();
            }
        } catch (oError) {
            if (oChannel) {
                oChannel["catch"](oError);
            } else {
                throw oError;
            }
        }
    };
}

通话结束后,恢复与下面的函数SAP代码:

_restoreRequestPrototype: function () {
    if (!XMLHttpRequest._SAP_ENHANCED) {
        return;
    }
    XMLHttpRequest.prototype.send = this.__send;
}
© www.soinside.com 2019 - 2024. All rights reserved.