提交基本身份验证详细信息

提交基本身份验证详细信息

本文介绍了SAPUI5 / AJAX,提交基本身份验证详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to access a SAP Successfactors API from an AJAX Call in a SAPUI5 application.

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

I can access the API fine using POSTMAN, and providing the Basic Authentication credentials.

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

How do I supply these credentials directly in AJAX. I've tried numerous ways from numerous post but no method seems to work.

Google Dev Tools的响应(控制台选项卡)

Response from Google Dev Tools (Console Tab)

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 Dev Tools的响应(网络标签)

Response from Google Dev Tools (Network Tab)

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

Ajax。

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);
                }

            });


推荐答案

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

The following issues might be the problem:

1)输入的用户名是否为USERNAME @ COMPANY:PASSWORD?

1) Is Username of Type: USERNAME@COMPANY:PASSWORD before sent?

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

2) Endpoint URL should be according to your data center, maybe DC2 is correct, but could also be DC12 ? https://api12.successfactors.eu/odata/v2/PerPerson?$select=personId instead of https://api2.successfactors.eu/odata/v2/PerPerson?$select=personId

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

3) Pass a reference to your success function

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避免跨域问题的正确方法!

4) Working with SAP Cloud Platform the right way to avoid cross-origin problems!

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

Destination (Connectivity -> Destinations) in SAP CP:

别忘了检查连接并接收HTTP状态代码= 200!

Don't forget to check the connection and receive HTTP status code = 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);

    }
});
});

这篇关于SAPUI5 / AJAX,提交基本身份验证详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:02