2

我正在尝试从 SAPUI5 应用程序中的 AJAX 调用访问 SAP Successfactors API。

我可以使用 POSTMAN 访问 API,并提供基本身份验证凭据。

如何直接在 AJAX 中提供这些凭据。我从众多帖子中尝试了多种方法,但似乎没有一种方法有效。

来自 Google 开发工具(控制台选项卡)的响应

Failed to load https://api2.successfactors.eu/odata/v2/PerPerson?$select=personId: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://webidetesting#####-#####.dispatcher.hana.ondemand.com' is therefore not allowed access.

来自 Google 开发工具(网络选项卡)的响应

Authentication credentials are required. Please provide a valid username, password and company id

阿贾克斯。

var aData = jQuery.ajax({
                type: "GET",
                contentType: "application/json",
                crossDomain: true,
                url: "https://api2.successfactors.eu/odata/v2/PerPerson?$select=personId",
                xhrFields: {
                    withCredentials: true
                },
                beforeSend: function (req) {
                    req.setRequestHeader('Authorization', 'Basic ' + btoa('Username:Password'));
                    req.setRequestHeader('Access-Control-Allow-Origin', '*');
                },
                headers: {
                    "Authorization": "Basic " + btoa("Username" + ":" + "Password"),
                    "Access-Control-Allow-Origin": "*"
                },
                username: "Username",
                password: "Password",
                dataType: "json",
                async: false,
                success: function (data, textStatus, jqXHR) {
                    oModel.setData({
                        modelData: data
                    });
                    alert("success to post");
                },
                error: function (oError) {
                    console.log(oError);
                }

            });
4

3 回答 3

2

以下问题可能是问题所在:

1) 用户名​​的类型是:USERNAME@COMPANY:PASSWORD 之前发送吗?

2) 端点 URL 应该根据您的数据中心,也许 DC2 是正确的,但也可能是 DC12 ?https:// api12 .successfactors.eu/odata/v2/PerPerson?$select=personId 而不是 https:// api2 .successfactors.eu/odata/v2/PerPerson?$select=personId

3)传递对您的成功函数的引用

var that = this;

....
success: function (data, textStatus, jqXHR) {
     var oModel = that.getView().getModel(); // get your model, instatiated outside this method
     oModel.setData({
        modelData: data
     });
     alert("success to post");
},
     error: function (oError) {
        console.log(oError);
}
....

4) 使用 SAP Cloud Platform 避免跨域问题的正确方法!

SAP CP 中的目标(连接 -> 目标):

不要忘记检查连接并接收 HTTP 状态码 = 200!

Name: sap_hcmcloud_core_odata, 
Type: HTTP
URL:  https://api12preview.sapsf.eu
Auth: Internet, Basic Authentication
  Your User (Username@Company), 
  Your Password
Properties  
  WebIDEEnabled = true
  WebIDESystem = SFSF
  WebIDEUsage = odata_gen

neo-app.json添加路由:

{ "path": "/sf-dest",
    "target": {
        "type": "destination",
        "name": "sap_hcmcloud_core_odata"
    },
    "description": "SFSF Connection"
}

在你的控制器中

sap.ui.define([
"sap/ui/core/mvc/Controller"], function (Controller) {
"use strict";

return Controller.extend("yourNamespace.yourAppName.controller.Main", {
    onInit: function () {
        var oModel = new sap.ui.model.json.JSONModel();
        var sHeaders = {
            "Content-Type": "application/json",
            "Accept": "application/json",
        };

        //sending request
        oModel.loadData("/sf-dest/odata/v2/PerPerson?$select=personId", null, true, "GET", null, false, sHeaders);
        console.log(oModel);

    }
});
});
于 2018-09-05T07:07:26.913 回答
0

答案是简单地创建一个没有身份验证的 DESTINATION 并在 AJAX 中应用所有授权

于 2018-09-06T09:26:39.027 回答
0

在这种情况下,一个 SCP 目的地是答案,但是如果有 2 个调用,一个调用身份验证 API(检索令牌),另一个调用 GET API(使用检索到的令牌作为身份验证?)

于 2018-09-05T12:47:59.863 回答