我正在尝试从 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);
}
});