【问题标题】:CORS Issue in SAP OData Service - No 'Access-Control-Allow-Origin' Header Is PresentSAP OData 服务中的 CORS 问题 - 不存在“Access-Control-Allow-Origin”标头
【发布时间】:2018-01-16 08:21:24
【问题描述】:

这个 CORS 问题陷入疯狂,没有解决方案。我正在 SAP HANA 中使用以下 xsaccess 文件内容创建 XSODOTA 服务,并在我的本地主机中使用 UI5 应用程序。

{
    "exposed": true,
    "authentication": [{
        "method": "Basic"
    }],
    "mime_mapping": [{
        "extension": "jpg",
        "mimetype": "image/jpeg"
    }],
    "force_ssl": false,
    "enable_etags": true,
    "prevent_xsrf": true,
    "anonymous_connection": null,
    "cors": [{
        "enabled": true,
        "allowMethods": [
            "GET",
            "POST",
            "HEAD",
            "OPTIONS"
        ],
        "allowOrigin": "*",
        "maxAge": "3600"
    }],
    "headers": {
        "enabled": true,
    },
    "cache_control": "no-cache, no-store",
    "default_file": "index.html"
}

我已经完成了所有这些,我可以从 XS 管理工具中的 SAP 论坛收集如下:

在jQuery ajax函数中调用服务如下:

$.ajax({
    type: "GET",
    url: "odataurl?$format=json",
    data: null,
    contentType: "application/json; charset=utf-8",
    jsonpCallback: 'processJSON',
    headers : {
        "Access-Control-Allow-Origin" : "*"
    },
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    },
    success: function(msg) {
        console.log(msg.d.results);
        that.onbindTable(msg.d.results);
    },
    error: function(err) {
    }
});

无法加载http://odataurl?$format=json:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:20019' 因此不允许访问。

【问题讨论】:

标签: jquery sapui5 hana


【解决方案1】:

您的允许标头列表似乎输入不正确。它们应该是每行一个条目,而您在一行中有一个长的逗号分隔的标题列表。这行不通。因此,无论是在您的 .xsaccess 文件中还是在 SAP HANA 管理工具中,您都需要将标头设置为单独的值。

例如,您的 .xsaccess 文件可能如下所示:

     "cors" :                      
            {
             "enabled" : true,
              "allowMethods": [
                                 "GET",
                                 "POST",
                                 "HEAD",
                                 "PUT",
                                 "OPTIONS"],
             "allowOrigin" : ["*"],
             "allowHeaders" : ["crossdomain","X-Requested-With","Access-Control-Request-Method","Access-Control-Request-Headers","Accept-Encoding","Sec-Fetch-Mode","Sec-Fetch-Site","authorization","Accept","Origin","Content-Type","Content-Length","content-encoding","Depth","User-Agent","Cache-Control","x-csrf-token","x-sap-cid","Accept-Language"],
             "exposeHeaders" : ["x-csrf-token","access-control-allow-headers","Access-Control-Allow-Origin","access-control-expose-headers","authorization"]
            }, 

【讨论】:

    【解决方案2】:

    试着把它放在 b1s.conf 正常的

    vim /usr/sap/SAPBusinessOne/ServiceLayer/conf/b1s.conf
    

    添加:

    {
      (...)
      "LicenseServer": "-----:40000",
    "CorsEnable": true,
     "CorsAllowedOrigins": "http://localhost:8080;http://other:8080"
    }
    

    然后

    /etc/init.d/b1s stop
    /etc/init.d/b1s start 
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2021-10-20
      • 2018-03-04
      • 2016-10-30
      • 1970-01-01
      • 2019-06-17
      • 2016-05-07
      • 2016-10-30
      相关资源
      最近更新 更多