javascript-如何使用SAP Leonardo的API转换?
作者:互联网
我需要有关SAP Leonardo的API转换的帮助.我构建了一个用于学习的翻译应用程序,并根据文档创建了一个翻译方法:
translate: function () {
//Create JSON Model with URL
var oModel = new sap.ui.model.json.JSONModel();
var langTo = this.getView().byId("idTo").getSelectedKey();
var langFrom = this.getView().byId("idFrom").getSelectedKey();
var textOld = this.getView().byId("idOldText").getValue();
//API Key for API Sandbox
var sHeaders = {
"Content-Type": "application/json",
"APIKey": "My api Key"
};
//Available Security Schemes for productive API Endpoints
//OAuth 2.0
//sending request
//API endpoint for API sandbox
var oData = {
"sourceLanguage": langTo,
"targetLanguages": [
langFrom
],
"units": [{
"value": textOld,
"key": "ANALYZE_SALES_DATA"
}]
};
oModel.loadData("https://sandbox.api.sap.com/ml/translation/translation", oData, true, "POST", null, false, sHeaders);
//Available API Endpoints
//https://mlfproduction-machine-translation.cfapps.eu10.hana.ondemand.com
//https://mlfproduction-machine-translation.cfapps.us10.hana.ondemand.com
//You can assign the created data model to a View and UI5 controls can be bound to it. Please refer documentation available at the below link for more information.
//https://sapui5.hana.ondemand.com/#docs/guide/96804e3315ff440aa0a50fd290805116.html#loio96804e3315ff440aa0a50fd290805116
//The below code snippet for printing on the console is for testing/demonstration purpose only. This must not be done in real UI5 applications.
oModel.attachRequestCompleted(function (oEvent) {
var oData = oEvent.getSource().oData;
// console.log(oData);
});
}
我使用了两个selectBox来获取语言键,它们都称为“ idTo”和“ idFrom”.而且我也使用输入来获取文本,将其翻译为id“ idOldText”.但是什么也没发生. oData值在最后一条指令中始终为空.我使用过SAP WEBIDE,我猜它不需要配置IDE即可使用该API.
有人可以帮我吗?
解决方法:
如果您从控制台提供错误,将很有帮助.
但是我已经感觉到,这最终会在跨站点请求中结束,因此会因为使用完整的合格URL而被阻止.另外,您的标头白名单可能会丢失.
这样做,它应该可以工作:
1)在SAP CP中创建目标
2)在SAP WebIDE中创建一个新的sapui5项目,并通过添加新的目标路径和标头将请求标头列入白名单来适应neo-app.json
{
"welcomeFile": "/webapp/index.html",
"routes": [{
"path": "/resources",
"target": {
"type": "service",
"name": "sapui5",
"entryPath": "/resources"
},
"description": "SAPUI5 Resources"
}, {
"path": "/test-resources",
"target": {
"type": "service",
"name": "sapui5",
"entryPath": "/test-resources"
},
"description": "SAPUI5 Test Resources"
}, {
"path": "/ml-dest",
"target": {
"type": "destination",
"name": "sapui5ml-api"
},
"description": "ML API destination"
}],
"sendWelcomeFileRedirect": true,
"headerWhiteList": [
"APIKey", "Accept", "Content-Type"
]
}
3)添加您的方法并发布请求||您的版本中可能存在的问题:JSON对象和请求标头
onInit: function () {
var oModel = new sap.ui.model.json.JSONModel();
var sHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
"APIKey": "<<yourAPIKey>>"
};
var oData = {
"sourceLanguage": "en",
"targetLanguages": [
"de"
],
"units": [{
"value": "I would like to analyze my sales data.",
"key": "ANALYZE_SALES_DATA"
}]
};
var ODataJSON = JSON.stringify(oData);
oModel.loadData("/ml-dest/translation/translation", ODataJSON, true, "POST", null, false, sHeaders);
oModel.attachRequestCompleted(function (oEvent) {
var oData = oEvent.getSource().oData;
console.log(oData.units[0].translations[0]);
});
}
4)加载应用程序时获得成功的响应对象:-)
使用的参考:
>目的地创建(我自己的博客条目顺便说一句)https://blogs.sap.com/2018/09/05/successfactors-extensions-with-sapui5-and-the-correct-usage-of-sap-cp-destination-services/
> SAP ML推理服务的SAPUI5示例(请参阅多个示例)https://developers.sap.com/tutorials/ml-fs-sapui5-img-classification.html
标签:sapui5,javascript 来源: https://codeday.me/bug/20191108/2008073.html